查询重复记录,求不同解法!!
SQL code
团号可以相同,查询所有记录,遇到团号相同,根据ID排序取唯一团号记录,
结果为:
SQL code
--ID 团号 姓名 --1 001 张三 --2 002 李四 --3 002 王五 --4 003 马六 --5 003 小东
团号可以相同,查询所有记录,遇到团号相同,根据ID排序取唯一团号记录,
结果为:
SQL code
--ID 团号 姓名 --1 001 张三 --2 002 李四 --4 003 马六
作者: goahead777 发布时间: 2011-06-16
SQL code
select * from tb a where not exists(select 1 from tb where 团号=a.团号 and id<a.id)
作者: zy112429 发布时间: 2011-06-16
SQL code
select * from tb a where id=(select min(id) from tb where 团号=a.团号)
作者: zy112429 发布时间: 2011-06-16
SQL code
select ID,团号,姓名 from (select row_number() over(partition by 团号 order by id) no,* from tb)a where no=1
作者: zy112429 发布时间: 2011-06-16
SQL code
--第1种 select * from tb a where not exists (select 1 from tb b where a.团号=b.团号 and b.ID<a.ID) --第2种 select * from tb a where id= (select top 1 id from tb b where a.团号=b.团号 order by b.ID) --第3种 select * from tb a where id= (select min(id) from tb b where a.团号=b.团号) --第4种 select * from tb a where id<= all (select id from tb b where a.团号=b.团号) --第4种 select * from tb where id in (select min(id) from tb group by 团号)
作者: cd731107 发布时间: 2011-06-16