如何获取具有多个员工编号的员工的重复姓名

时间:2011-10-25 18:36:30

标签: sql oracle

我正在使用Oracle 10g。

如果我有以下重复行(具有两个员工编号的相同员工):

Employee_No      Employee_Name      ID_NO
----------------------------------------------
0002345          John Debb          100345642
0030988          John Debb          100345642
----------------------------------------------

我希望得到结果:

Employee_No_1      Employee_No_2     Employee Name    ID_NO
----------------------------------------------------------------
0002345            0030988           John Debb        100345642
----------------------------------------------------------------

是否可以在SQL中完成?还是需要PL / SQL?查询是什么?

3 个答案:

答案 0 :(得分:6)

SELECT MIN(Employee_no), MAX(employee_no), Employee_name, id_no
FROM Employee
GROUP BY Employee_name, id_no
HAVING MIN(employee_no) <> MAX(employee_no)

我不做Oracle,但我认为这是非常通用的语法,应该有用。

答案 1 :(得分:3)

不完全符合要求的格式,但是这将处理不止两个重复的情况。

SELECT e.Employee_No, e.Employee_Name, e.ID_NO
    FROM (SELECT Employee_Name, ID_NO
              FROM Employee
              GROUP BY Employee_Name, ID_NO
              HAVING COUNT(*) > 1) q
        INNER JOIN Employee e
            ON q.Employee_Name = e.Employee_Name
                AND q.ID_NO = e.ID_NO
    ORDER BY e.Employee_Name, e.ID_NO, e.Employee_No

答案 2 :(得分:1)

查询如下,

select e1.employee_no, e2.employee_no, e1.employee_name, e1.id_no
from employee e1
join employee e2
    on e1.id_no = e2.id_no
where e1.employee_no < e2.employee_no