SQL Server查找重复项

时间:2012-03-07 18:06:42

标签: sql-server

说我有一张表如下

Employee    RecNumber
Joe Bloggs  123456
Joe Bloggs  123456
Bob Bloggs  123457
Dup Bloggs  123456

我只想返回所有Rec Number,其中2人拥有相同的RecNumber,这不应该发生。 请注意,一个人可以多次拥有相同的请求编号,我只想要返回,其中2个人具有相同的编号

所以我想要的就是

123456

5 个答案:

答案 0 :(得分:2)

select record_number
  from my_table
  group by record_number
  having count(distinct employee) > 1

答案 1 :(得分:0)

SELECT A.RECNUMBER,COUNT(B.EMPLOYEE)
(SELECT DISTINCT RECNUMBER FROM YOURTABLE) A,
YOURTABLE B
WHERE A.RECNUMBER = B.RECNUMBER
GROUP BY A.RECNUMBER
HAVING COUNT(B.EMPLOYEE)=2;

答案 2 :(得分:0)

这样的东西?

SELECT RecNumber FROM Table GROUP BY RecNumber HAVING COUNT(*) > 1

答案 3 :(得分:0)

SELECT RecNumber FROM [a table] 
 GROUP BY RecNumber HAVING COUNT(*) > 1;

或者

SELECT RecNumber FROM (SELECT RecNumber, Employee
  FROM [a table] 
 GROUP BY RecNumber, Employee HAVING COUNT(*) > 1
) AS x;

答案 4 :(得分:0)

你可以写类似的东西,我认为它有效:

use *DATABASE_NAME*
go
select *Your_Field*, Nbre=count(*Your_Field*) from *TABLE_NAME* 
group by *Your_Field*
having count(*Your_Field*)>1
order by 2 desc

享受