62,254
社区成员
发帖
与我相关
我的任务
分享DELETE FROM tab
WHERE tab.ROWID IN (SELECT B.RID
FROM (SELECT tab.*,
tab.ROWID RID,
ROW_NUMBER() OVER(PARTITION BY col1, col2, col3 ORDER BY col1, col2, col3) RN
FROM tab) B
WHERE RN <> 1)
----测试结果
(所影响的行数为 7 行)
col1 col2 col3
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
111 aaa ok
111 aaa no
111 aaa ok
222 aaa ok
222 aaa no
(所影响的行数为 5 行)
col1 col2 col3
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
111 aaa no
111 aaa ok
111 bbb no
222 aaa no
222 aaa ok
333 bbb ok
(所影响的行数为 6 行)
--测试数据, ascor,09-12-24
declare @t table (col1 varchar(50),col2 varchar(50),col3 varchar(50))
insert into @t
select '111','aaa','ok' union all
select '222','aaa','ok' union all
select '333','bbb','ok' union all
select '111','bbb','no' union all
select '111','aaa','no' union all
select '222','aaa','no' union all
select '111','aaa','ok'
---- 1
select a.col1,a.col2,a.col3
from @t a inner join
(
select col1 ,col2
from @t
group by col1 ,col2
having(count(1))>1
) as b
on a.col1=b.col1 and a.col2=b.col2
---- 2
select distinct col1,col2,col3 from @t
declare @t table (col1 varchar(50),col2 varchar(50),col3 varchar(50))
insert into @t
select '111','aaa','ok' union all
select '222','aaa','ok' union all
select '333','bbb','ok' union all
select '111','bbb','no' union all
select '111','aaa','no' union all
select '222','aaa','no' union all
select '111','aaa','ok'
select * from @t where col1 in
(select col1 from @t group by col1,col2 having count(*)>1)
and col2 in
(select col2 from @t group by col1,col2 having count(*)>1)
select distinct col1,col2,col3 from @tselect col1 ,col2 from tablename
group by col1 ,col2
having(count(*))>1select * from tab inner join (select count(*) as c, col1 from tab) tmp
on tab.col1 = tmp.col1 and tmp.c >1select distinct * from tab