Oracle Beginner在这里尝试从每家商店返回收入最高的员工。是否可以通过执行INNER JOIN
ON MAX
/ SAL+COMM
表达式和第二列(store_id
)来返回此结果,其格式类似于下面的第一个查询(两个 - 列连接)?替代解决方案返回我想要的结果,但仅基于总薪资,允许在更大的数据集中产生无关的结果。
ENAME SAL COMM STORE_ID
KING 5000 - 3
BLAKE 2850 - 3
CLARK 2450 - 3
JONES 2975 - 1
SCOTT 3000 400 4
FORD 3000 0 4
SMITH 800 700 4
SELECT store_id, ename, (salary+NVL(commission,0)) total_pay
FROM employees E1
INNER JOIN
(SELECT store_id, MAX(sal+NVL(comm,0)) highest_paid
FROM employees
GROUP BY store_id) E2
ON E1.store_id = E2.store_id
AND total_pay = highest_paid
替代解决方案
SELECT *
FROM EMPLOYEES
WHERE (SAL+NVL(COMM,0)) IN (SELECT MAX(SAL+NVL(COMM,0))
FROM EMPLOYEES
GROUP BY store_id)
答案 0 :(得分:0)
我不确定你在寻找什么,但表达这种逻辑的一种非常自然的方式是使用窗口函数:
SELECT storeid, empname, agg
FROM (SELECT e.*, (salary+ COALESCE(commission, 0)) AS agg,
MAX(salary + COALESCE(commission, 0)) OVER (PARTITION BY storeid) as max_agg
FROM table.employees e
) e
WHERE agg = max_agg;