SQL server:比较行

SQL server:比较行,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我需要一个可以执行以下操作的查询。 我有一个有两列的表 ID Values 1 1 1 2 1 3 1 4 2 2 2 5 2 6 如果你看到ID1的值是1,2,3和4,ID2的值是2,5和6 我想写一个返回以下内容的查询 一,- 4- 5+ 六,+ 通过比较两个ID,删除平均值1和4,添加平均值5和6 可能吗?请让我知道 谢谢类似于: ( SELECT T.Value FROM dbo.Table T WHERE T.ID = 1 EXCEPT SELECT T.

我需要一个可以执行以下操作的查询。 我有一个有两列的表

ID Values
 1  1
 1  2
 1  3
 1  4
 2  2
 2  5
 2  6
如果你看到ID1的值是1,2,3和4,ID2的值是2,5和6

我想写一个返回以下内容的查询

一,- 4- 5+ 六,+

通过比较两个ID,删除平均值1和4,添加平均值5和6

可能吗?请让我知道

谢谢

类似于:

(
SELECT T.Value FROM dbo.Table T WHERE T.ID = 1
EXCEPT
SELECT T.Value FROM dbo.Table T WHERE T.ID = 2
)
UNION
(
SELECT T.Value FROM dbo.Table T WHERE T.ID = 2
EXCEPT
SELECT T.Value FROM dbo.Table T WHERE T.ID = 1
)

这将获得与1(但不是2)和2(但不是1)关联的值列表。您可以轻松地将其中一个子查询的值乘以-1以区分它们,或者将它们作为两个单独的查询运行。

这将为您提供1&4:

select a.values
from my_table a
where not exists (
select * from my_table b where b.values = a.values and b.ID = 2)
and a.ID = 1
这将为您提供5和6:

select a.values
from my_table a
where not exists (
select * from my_table b where b.values = a.values and b.ID = 1)
and a.ID = 2

感谢您的快速回复:-