员工的架构
(ID, EMPLOYEENAME, SALARY, ORGANIZATIONID)
要解决的查询:在每个单位中查找员工姓名,其中最高工资没有加入。
SELECT E.*
FROM EMPLOYEE E,
(SELECT EMP.ORGANIZATIONID, MAX(EMP.SALARY)
FROM EMPLOYEE EMP
GROUP BY EMP.ORGANIZATIONID) MAXSALARY
WHERE MAXSALARY.SALARY =E.SALARY
AND E.ORGANIZATIONID=EMP.ORGANIZATIONID ;
有没有办法避免加入?我正在使用Spark SQL API和连接导致额外的shuffle操作,这是昂贵的。有没有办法在获得最高薪水时获得员工姓名?
假设每个组织中有一名员工拥有最高薪水
答案 0 :(得分:1)
您可以将PARTITION BY
与Spark SQL一起使用,如下所示(虽然它需要子查询)
SELECT E.*
FROM
(SELECT EMP.EMPLOYEENAME, EMP.ORGANIZATIONID, EMP.SALARY,
row_number() OVER (PARTITION BY ORGANIZATIONID ORDER BY SALARY DESC) as rank
FROM EMPLOYEE EMP
) AS E
WHERE E.rank=1
答案 1 :(得分:0)
试试这个:
SELECT P.ORGANIZATIONID, P.EMPLOYEENAME
FROM EMPLOYEE P
WHERE P.SALARY = (SELECT MAX(E.SALARY) FROM EMPLOYEE E WHERE P.ORGANIZATIONID = E.ORGANIZATIONID)
GROUP BY P.ORGANIZATIONID, P.EMPLOYEENAME
答案 2 :(得分:0)
试试这个:
SELECT EMPLOYEENAME FROM EMPLOYEE
WHERE SALARY IN (SELECT MAX(SALARY) FROM EMPLOYEE GROUP BY ORGANIZATIONID)