Oracle在一个查询中显示min(),max()

时间:2016-08-21 13:24:48

标签: sql database oracle

我有桌子---- 作品(employer_name,employee_name,薪水)

我希望结果显示每个雇主的最低和最高工资组,请参阅下文。

作品

Employer Employee Max Min
A        B        Y
A        C             Y
N        K        Y    
N        P             Y

有什么想法吗?非常感谢。

干杯, 本

1 个答案:

答案 0 :(得分:0)

这个怎么样?这不完全是你要求的,但应该做得很好:)

SELECT w.*,
    (SELECT employee_name FROM works WHERE employer_name = w.Employer AND salary = w.Min AND ROWNUM <= 1) AS EmployeeMin,
    (SELECT employee_name FROM works WHERE employer_name = w.Employer AND salary = w.Max AND ROWNUM <= 1) AS EmployeeMax
FROM (
    SELECT employer_name AS Employer,
        min(salary) AS Min,
        max(salary) AS Max
    FROM works
    GROUP BY Employer
) AS w