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

怎么distinct去除多余行

2012-03-15 
如何distinct去除多余行?idc1c2c3c41124Peter2124Wang3124David4134Ben5134Su6256Ru7256Song简单数据如上

如何distinct去除多余行?
id               c1               c2             c3           c4
  1                 124           Peter
  2                 124           Wang
  3                 124           David
  4                 134           Ben
  5                 134           Su
  6                 2               5                 6             Ru
  7                 2               5                 6             Song
简单数据如上
想查出结果:
id               c1               c2               c3           c4
1                   1                 2                 4             Peter
2                   1                 3                 4             Ben
3                   2                 5                 6             Ru
也就是c1,c2,c3三列数据有重复行,如何消除重复,只保留重复的一行?
各位帮忙呀!!


[解决办法]
才看出来id需要排序,要是写的话太长,用这个改一下可得到顺序id
select id= (select count(*) from b where id> = c.id),c.c1,c.c2,c.c3.c.c4 from b c

[解决办法]
select * from tb a
where not exists(select * from tb where c1=a.c1 and c2=a.c2 and c3=a.c3 and id <a.id )

热点排行