读书人

求句SQL.等马上结帖,该如何处理

发布时间: 2012-01-08 22:48:50 作者: rapoo

求句SQL..等,马上结帖
TABLE
---------------
ID | NAME
---------------
A 110
A 120
B 110
B 120
C 110
D 120
E 120

查询结果应为
---------------
ID | NAME
---------------
A 110
B 110
C 110
D 120

SQL如何写?

[解决办法]
--创建测试数据
declare @t table(ID char(1),NAME int)
insert @t select 'A ',110
union all select 'A ',120
union all select 'B ',110
union all select 'B ',120
union all select 'C ',110
union all select 'D ',120
union all select 'E ',120
--查看测试数据
select * from @t
--查看结果
select * from @t a where not exists
(select 1 from @t where id=a.id and name <a.name)
/*
ID NAME
---- -----
A 110
B 110
C 110
D 120
E 120

(所影响的行数为 5 行)
*/

读书人网 >SQL Server

热点推荐