查询在表格中插入工资部门的总和

时间:2015-02-20 07:47:34

标签: sql oracle

假设我有一个数据为

的表
EMPNO   ENAME   SAL DEPTNO  TOTAL
7839    KING    5000    10
7698    BLAKE   2850    30
7782    CLARK   2450    10
7566    JONES   2975    20
7788    SCOTT   3000    20
7902    FORD    3000    20
7369    SMITH   800     20
7499    ALLEN   1600    30
7521    WARD    1250    30
7654    MARTIN  1250    30
7844    TURNER  1500    30
7876    ADAMS   1100    20
7900    JAMES   950     30
7934    MILLER  1300    10

现在我想在TOTAL部门明智的列中插入薪水总和

EMPNO   ENAME   SAL DEPTNO  TOTAL
7839    KING    5000    10  8750
7698    BLAKE   2850    30  9400
7782    CLARK   2450    10  8750
7566    JONES   2975    20  10875
7788    SCOTT   3000    20  10875
7902    FORD    3000    20  10875
7369    SMITH   800     20  10875
7499    ALLEN   1600    30  9400
7521    WARD    1250    30  9400
7654    MARTIN  1250    30  9400
7844    TURNER  1500    30  9400
7876    ADAMS   1100    20  10875
7900    JAMES   950     30  9400
7934    MILLER  1300    10  8750

请帮助我并指导我。

2 个答案:

答案 0 :(得分:1)

试试这个:

  Update table_name t2 set t2.TOTAL= (select t1.temp  from 
  (select DEPTNO ,sum(SAL) temp from table_name  group by DEPTNO) t1 where 
  t1.DEPTNO=t2.DEPTNO);

答案 1 :(得分:1)

您可以使用按部门分组的分析功能 SUM OVER

SQL> SELECT t.empno, t.ename, t.sal, t.deptno,
  2  sum(sal) OVER(PARTITION BY deptno) total
  3  FROM emp t
  4  /

     EMPNO ENAME             SAL     DEPTNO      TOTAL
---------- ---------- ---------- ---------- ----------
      7782 CLARK            2450         10       8750
      7839 KING             5000         10       8750
      7934 MILLER           1300         10       8750
      7566 JONES            2975         20      10875
      7902 FORD             3000         20      10875
      7876 ADAMS            1100         20      10875
      7369 SMITH             800         20      10875
      7788 SCOTT            3000         20      10875
      7521 WARD             1250         30       9400
      7844 TURNER           1500         30       9400
      7499 ALLEN            1600         30       9400
      7900 JAMES             950         30       9400
      7698 BLAKE            2850         30       9400
      7654 MARTIN           1250         30       9400

14 rows selected.

SQL>