+ -
当前位置:首页 → 问答吧 → 问个SQL查寻问题。删出重复项问题,要查寻语句。

问个SQL查寻问题。删出重复项问题,要查寻语句。

时间:2011-11-16

来源:互联网

a列 B列 c列
1 2 3
2 2 5
3 2 6
1 2 6


要求:
删除这份表中A列为重复的行。就是把A列中为“1”的行删出,为“1”的行随便保留一行即可。

想要的结果为:
a列 B列 c列
1 2 3
2 2 5
3 2 6

或:

2 2 5
3 2 6
1 2 6



谢谢


作者: sxssg   发布时间: 2011-11-16

SQL code
select * from tb a where not exists(select 1 from tb where a=a.a and c<a.c)

作者: qianjin036a   发布时间: 2011-11-16

select t.* from tb t where c = (select min(c) from tb where a = t.a)
select t.* from tb t where c = (select max(c) from tb where a = t.a)
select t.* from tb t where not exists (select 1 from tb where a = t.a and c < t.c)
select t.* from tb t where not exists (select 1 from tb where a = t.a and c > t.c)
select t.* from tb t where c = (select top 1 c from tb where a = t.a and order by c)
select t.* from tb t where c = (select top 1 c from tb where a = t.a and order by c desc)

作者: dawugui   发布时间: 2011-11-16

SQL code
delete a from table1 as a where exists(select 1 from table1 where a=a.a and c>a.c)

作者: roy_88   发布时间: 2011-11-16

SQL code
--> --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([a] int,[B] int,[c] int)
Insert #T
select 1,2,3 union all
select 2,2,5 union all
select 3,2,6 union all
select 1,2,6
Go
delete a
from (Select * ,row=row_number()over(partition by a order by newid())from #T)a
where row>1
select * from #T

作者: roy_88   发布时间: 2011-11-16

保留A列相同的,任何一条记录可用以上方法

作者: roy_88   发布时间: 2011-11-16