日期:2014-05-18  浏览次数:20540 次

弱问sql简单问题
我想从表中删除所有不同value的行,但是具有相同value的行只删除第一行,请问如何操作?

------解决方案--------------------
SQL code

create table bs(value varchar(1))

insert into bs
select 'a' union all
select 'b' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'c'

select value from bs

value
-----
a
b
b
c
c
c


-- 执行一次操作后,表变成{b,c,c}
;with t as
( select row_number() over(partition by value order by (select 0)) rn
  from bs
)
delete from t where rn=1

select value from bs 

value
-----
b
c
c


-- 再执行一次,变成{c}
;with t as
( select row_number() over(partition by value order by (select 0)) rn
  from bs
)
delete from t where rn=1

select value from bs 

value
-----
c