sql输出重复数据,分几次执行

ID a b c

1 1 2 3

2 2 2 3

3 1 2 3

4 2 2 3

5 2 1 3

6 1 1 3

7 3 2 1

        输出a,b,c 值均相同的数据   
            该表用sql,首先输出1 1 2 3  和3 1 2 3结束
            然后再执行,输出 2 2 2 3 和4 2 2 3

select * from [表A] where id in (select id from [表A] group by id having count(id) >1 )

感觉其实完全可以distinct出来后生成新的table,比较两张table,不同的就是重复的。

abc_test换成你实际的表名,以下,重复执行带出abc相同的行直到没有重复为止时,得到‘NA’:
if not exists(select 0 from tempdb..sysobjects where id=object_id('tempdb..#abc_acount'))
select 0 'rid' into #abc_acount
if not exists(select 0 from tempdb..sysobjects where id=object_id('tempdb..#abc_last_id'))
select row_number() over(order by min(id)) rid,null tid,count(*) cn into #abc_last_id from abc_test group by a,b,c having count(*)>1 order by min(id);
if not exists(select 0 from tempdb..sysobjects where id=object_id('tempdb..#abc_groups'))
select tba.mid,tbb.* into #abc_groups from (select min(id) mid,a a1,b b1,c c1 from abc_test group by a,b,c having count(*)>1) tba inner join abc_test tbb on tba.a1=tbb.a and tba.b1=tbb.b and tba.c1=tbb.c order by tba.mid
if (select min(tid) from #abc_last_id) is not null begin delete from #abc_groups where mid=(select min(tid) from #abc_last_id)
delete from #abc_last_id where rid=(select rid from #abc_acount) end
update #abc_last_id set tid=(select min(mid) from #abc_groups)
update #abc_acount set rid=rid+1
if exists(select 0 from #abc_last_id)
select top (select top 1 cn from #abc_last_id order by rid) id,a,b,c from #abc_groups else select 'NA'

运行以下语句可复位,即重新运行上面语句为第一次执行判定:
drop table #abc_last_id
drop table #abc_groups
drop table #abc_acount

  1. 先求出a,b,c均相同的数据,并按最小的id排序
    select concat(a,'#',b,'#',c) as abc, count(*) as count, min(id) as minid from A group by concat(a,'#',b,'#',c) having count>1 order by minid
    命名为B

  2. 每次取B表的一行做为条件,在A表里查就行了
    var @index=0;
    select A.* from A join (select * from B limit @index,1) on concat(a,'#',b,'#',c)=B.abc

如果你希望的是sql不变,执行一次出第一批符合条件的,第二次执行出第二批符合条件的,那是不可能的