更新Oracle 11g HR模式的列值

时间:2018-11-06 12:46:44

标签: sql oracle oracle11g

我正在尝试将所有员工的薪水更新为其工作所在部门的最高薪水。

ERD: enter image description here

我的尝试:

UPDATE EMPLOYEES SET
(SELECT SALARY
FROM EMPLOYEES
GROUP BY SALARY, DEPARTMENT_ID) = 
(SELECT "MAX"(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID);

显然这是错误的。它给出了错误:

ORA-01747: invalid user.table.column, table.column, or column specification

如何正确执行此操作?

2 个答案:

答案 0 :(得分:1)

您可以使用相关子查询:

UPDATE EMPLOYEES
    SET SALARY = (SELECT MAX(SALARY)
                  FROM EMPLOYEES E2
                  WHERE E2.DEPARTMENT_ID = EMPLOYEES.DEPARTMENT_ID
                 );

答案 1 :(得分:0)

您可以使用merge

   MERGE INTO EMPLOYEES a
    USING ( SELECT DEPARTMENT_ID, MAX(SALARY) as sal
                      FROM EMPLOYEES group by DEPARTMENT_ID
         ) b
    ON ( a.DEPARTMENT_ID = b.DEPARTMENT_ID )
    WHEN MATCHED THEN 
    UPDATE SET  a.SALARY = b.sal