读书人

如何在一个表里查找字段A相同而在字段

发布时间: 2012-10-15 09:45:25 作者: rapoo

怎么在一个表里查找字段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]gocreate table [tb]([A] int,[B] int)insert [tb]select 1,1 union allselect 1,2 union allselect 2,3 union allselect 2,3 union allselect 2,4 union allselect 3,5 union allselect 3,5 union allselect 3,5 union allselect 3,6go;with cte as(select rn=row_number() over(order by getdate()),* from tb)select a.a,a.bfrom cte ajoin (select min(rn) as rn,a,b from cte group by a,b) bon a.a=b.a and a.b=b.b and a.rn!=b.rn/**a           b----------- -----------2           33           53           5(3 行受影响)**/ 

读书人网 >SQL Server

热点推荐