oracle—如何使用SRF执行sql查询并只显示不同的值?

iyfjxgzm  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(421)

我正在考虑一种方法,用单行函数进行查询,只显示不同的值。假设我有一个包含employee、store和salary列的employees表,我想使用srfmax(salary)找出每个商店中薪水最高的员工。如果一个商店有超过一名员工赚取最高工资,我如何避免在每个商店显示超过一名最高收入者?请参见下面的代码。越简单越好。谢谢您!

SELECT employee, emp1.store, emp1.salary
FROM employees emp1
INNER JOIN (SELECT store, salary, MAX(salary) FROM employees GROUP BY store) emp2
ON emp1.store = emp2.store
AND emp1.salary = emp2.salary
fbcarpbf

fbcarpbf1#

可能有一种更干净的方法:

select * from 
    (
       SELECT employee, emp1.store, emp1.salary,
       ROW_NUMBER( ) 
       OVER ( partition by emp1.store order by emp1.salary desc ) rn
       FROM employees emp1
       INNER JOIN (SELECT store,  salary, MAX(salary) FROM employees GROUP BY store, salary) emp2
    ON emp1.store = emp2.store
    AND emp1.salary = emp2.salary
)
where rn = 1

row_number()解析函数将赋值为1,2,3,。。。按商店分组。然后总是为每个分组选择第1行。

相关问题