如何在oracle中查找组中每个第一行的所有列

时间:2015-01-18 22:16:33

标签: sql oracle greatest-n-per-group

EmployeeID  EmployeeName 
0001          Mr.A  
0002          Mr.A
0003          Mr.A
0004          Mr.B
0005          Mr.B 
0006          Mr.B
0007          Mr.C
0008          Mr.C 
0009          Mr.C

Expected Output 
0001 Mr.A
0004 Mr.B
0007 Mr.c

我希望只看到第一次出现员工ID的唯一员工姓名。

2 个答案:

答案 0 :(得分:1)

您可以使用row_number()

执行此操作
select t.*
from (select t.*,
             row_number() over (partition by EmployeeName order by EmployeeId) as seqnum
      from table t
     ) t
where seqnum = 1;

答案 1 :(得分:0)

CTE使用ROW_NUMBER

的另一个可能答案
WITH C AS(
    SELECT ROW_NUMBER() OVER (PARTITION BY EmployeeName ORDER BY EmployeeId) AS Rn
   ,EmployeeId
   ,EmployeeName
)
SELECT * FROM C WHERE Rn = 1