我有一张桌子(员工)。
员工
ID Name Salary Department
------------------------------
1 Steve 10000 SQ
2 Buck 15000 AS
3 Dan 10000 SQ
4 Dave 10000 AS
5 Jack 30000 AS
6 Amy 8000 GM
我需要为每个部门返回一名员工。例如,对于上述日期,我需要返回如下数据:
ID Name Salary Department
------------------------------
1 Steve 10000 SQ ( I can either return Steve or Dan for this group SQ)
2 Buck 15000 AS ( I can either return Buck or Dave or Jack for this group AS)
6 Amy 8000 GM
答案 0 :(得分:5)
这适用于row_number()
:
select id, name, salary, department
from (select t.*,
row_number() over (partition by department order by department) as seqnum
from t
) t
where seqnum = 1
如果您想要第一个或最后一个ID,请在order by id asc
分区子句中使用order by id desc
或row_number()
。如果您想要一个随机行,请使用order by dbms_random.value
。