SQL用于选择一列中具有重复值的所有行

时间:2016-04-03 10:09:49

标签: sql postgresql select duplicates

我的users表格在employee_id列中有重复值。我需要列出所有具有重复employee_ids的行及其名称。我需要查看所有具有重复employee_id的用户,以便我可以解除哪些值有效。

SELECT name,employee_id  
FROM users;

name     | employee_id
-----------------------
jason      12345
tom        34567 
jane       12345
will       01020
bill       12345

我需要回复:

name       | employee_id
-------------------------
jason        12345
jane         12345
bill         12345

我看到类似的问题,但我仍然无法获得我需要的正确语法。以下我只会发生一次。我需要像上面一样重复employee_id的所有事件。

SELECT employee_id, MAX(name) 
FROM users 
GROUP BY employee_id 
HAVING COUNT(employee_id) > 1;

1 个答案:

答案 0 :(得分:14)

您可以使用窗口COUNT

SELECT sub.name, sub.employee_id
FROM (SELECT *, COUNT(*) OVER(PARTITION BY employee_id) AS c
      FROM users) AS sub
WHERE c > 1;

LiveDemo

或简单IN

SELECT *
FROM users
WHERE employee_id IN (SELECT employee_id 
                      FROM users 
                      GROUP BY employee_id 
                      HAVING COUNT(employee_id) > 1);

LiveDemo2

或相关子查询:

SELECT name, employee_id
FROM users u
,LATERAL (SELECT COUNT(*) FROM users u2 WHERE u.employee_id = u2.employee_id) AS s(c)
WHERE c > 1;

SqlFiddleDemo