首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-08 
求句SQL..等,马上结帖TABLE---------------ID|NAME---------------A110A120B110B120C110D120E120查询结果

求句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 行)
*/

热点排行