日期:2014-05-17  浏览次数:20348 次

怎么在一个表里查找字段A相同而在字段B里有重复的记录
比如
A B
1 1
1 2
2 3
2 3 //和上面有重复了
2 4
3 5
3 5
3 5 //有3个重复了
3 6
我要找出有重复的列出来
A B
2 3
3 5
3 5

------解决方案--------------------
你看看这个博客,读懂了应该可以解决你的问题。
http://blog.csdn.net/yangsh0722/article/details/8021246
------解决方案--------------------
SQL code
select a,b from tb group by a,b having count(*)>1

------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([A] int,[B] int)
insert [tb]
select 1,1 union all
select 1,2 union all
select 2,3 union all
select 2,3 union all
select 2,4 union all
select 3,5 union all
select 3,5 union all
select 3,5 union all
select 3,6
go

;with cte as
(select rn=row_number() over(order by getdate()),* from tb)

select a.a,a.b
from cte a
join (select min(rn) as rn,a,b from cte group by a,b) b
on a.a=b.a and a.b=b.b and a.rn!=b.rn

/**
a           b
----------- -----------
2           3
3           5
3           5

(3 行受影响)
**/