如何在数据库表中查找重复记录?

时间:2014-02-14 04:16:18

标签: sql sql-server sql-server-2008

我有一张有员工的桌子和另一张有州的桌子。

我想为每个州找到重复的员工。

我的代码:

SELECT 
    mem.Employee AS emp,  COUNT(*) AS dupeCount     
FROM 
    MEMtable AS mem 
INNER JOIN 
    RTable AS Routing ON mem.MemberID = Routing.MemberKey 
GROUP BY 
    mem.employee
HAVING  
    COUNT(*)>1

对于同样的两张桌子,我必须再次找到没有任何雇员的州。

 SELECT routing.state AS empt     
 FROM MEMtable AS mem 
 INNER JOIN RTable AS Routing ON mem.MemberID = Routing.MemberKey 
 WHERE mem.employee IS NULL

5 个答案:

答案 0 :(得分:2)

SELECT Routing.[State],mem.Employee AS emp, COUNT(*) AS dupeCount 
FROM MEMtable AS mem 
INNER JOIN RTable AS Routing ON mem.MemberID =Routing.MemberKey 
GROUP BY Routing.[State],mem.employee HAVING COUNT(*)>1

正如你在评论中提出的第二个问题 它非常简单,只需检查Routing.MemberKey为null,无需加入其他表,如果只是为了从RTable中选择数据

SELECT routing.state    
 FROM RTable AS Routing  
 WHERE ISNULL(Routing.MemberKey,'')=''

答案 1 :(得分:1)

此查询将为您提供多个员工。

SELECT
 Routing.[State],mem.Employee AS emp, Count(*) as NoOfRecord
FROM MEMtable AS mem 
INNER JOIN RTable AS Routing ON mem.MemberID =Routing.MemberKey 
GROUP BY Routing.[State],mem.employee

答案 2 :(得分:0)

要查找没有员工的州,您需要完整的州名单。您的表格显示为“ mem ”(员工列表)和“路由”(包含员工的州列表)。无论是否有员工,您都没有所有州的清单。

例如:

<强> MEM

 - memberid=100, emp=John
 - memberid=200, emp=Bob
 - memberid=300, emp=Frank

<强> ROUTING

 - memberkey=200, state=California
 - memberkey=300, state=Kansas
 - memberkey=100, state=Michigan

你能否回答这个问题:“纽约有多少员工?” 不,因为纽约没有ROUTING行。

如果您的ROUTING表包含纽约的条目,那么memberKey会是什么?

 - memberkey=???, state=NewYork

因此,您可以了解每个州至少有一名员工在该州的员工人数。但你无法找出哪个州没有雇员,因为你没有州的记录,除非你在该州至少有一名雇员。

修改

如果我们假设每个状态在ROUTING表中至少列出一次(可能将memberkey设置为NULL或某些不存在的员工ID),那么您将在ROUTING表中查找没有状态的任何状态在MEM表中匹配员工。 LEFT JOIN用于此目的:

  SELECT Routing.state, 
          count(mem.MemberID) empCount 
   FROM RTable Routing
   LEFT JOIN MEMTable mem
     ON mem.MemberID = Routing.MemberKey
   GROUP by mem.state
   HAVING count(mem.MemberID) = 0;

说明:

查询返回ROUTING表中每个州的员工数。 ROUTING表与MEM表匹配。通过使用LEFT JOIN运算符,如果没有匹配,mem.MemberID列将返回NULL值,如果匹配则返回正常值。 COUNT()函数只计算非空值。因此,如果状态没有匹配,则COUNT将为零。在按状态对数据进行分组并计数之后,使用HAVING子句对此进行测试。任何计数为零的州都没有雇员。

这不会考虑ROUTING表中根本没有列出的状态。

答案 3 :(得分:0)

尝试第二个问题

SELECT routing.state AS empt     
 FROM MEMtable AS mem 
 RIGHT JOIN RTable AS Routing ON mem.MemberID =Routing.MemberKey 
 where mem.employee is null

答案 4 :(得分:0)

这可能会回答你的第二个问题......

SELECT routing.state AS empt     
 FROM  RTable AS Routing  
 LEFT JOIN  MEMtable AS mem ON mem.MemberID =Routing.MemberKey 
 where mem.employee is null