-1

考虑表格employee

desc employee;

Name                       Null?    Type
-------------------------- -------- ------------
EMPLOYEENO                 NOT NULL NUMBER(4)
ENAME                               VARCHAR2(15)
JOB                                 VARCHAR2(15)
MGR                                 NUMBER(4)
HIREDATE                            DATE
SAL                                 NUMBER
COMM                                NUMBER
DEPTNO                              NUMBER(2)

包含 的值deptnosal如:

    DEPTNO      SAL
---------- ----------
        10     2450
        10     5000
        10     1300
        20     2975
        20     3000
        20     1100
        20      800
        20     3000
        30     1250
        30     1500
        30     1600
        30      950
        30     2850
        30     1250

需要用他们部门的平均工资来更新员工的工资。即将每个员工的工资设置为部门平均水平。group by 条款是否有任何解决方案,例如

update employee set sal =(select avg(sal) from employee group by deptno) where deptno in(select deptno from employee group by deptno)

那么如何查询相同。

4

3 回答 3

3

试试这个

UPDATE EMPLOYEE A
SET SAL = (SELECT AVG(SAL) FROM EMPLOYEE B WHERE B.DEPTNO = A.DEPTNO);
于 2013-01-27T06:28:14.877 回答
0
update employee a
set sal =  (select avg(sal) as avgsal
          from employee b
          where A.DEPTNO = b.deptno
        );
于 2013-01-27T06:29:17.333 回答
0

这将作为 MERGE 更有效地实现,其中 USING 子句将所有部门的薪水汇总到部门级别,并连接到 department_id 上的员工表。

于 2013-01-27T10:44:33.663 回答