SELECT sql返回3行列中的值之和作为第二列值,依此类推

时间:2015-12-04 09:34:57

标签: sql oracle sum

考虑一下

select first_name, salary
from employees;
FIRST_NAME               SALARY
-------------------- ----------
Jack                       8400
Kimberely                  7000
Charles                    6200
Winston                    3200
Jean                       3100
Martha                     2500
Girard                     2800
Nandita                    4200

我需要3个工资的总和作为SUM列值。

预期结果:

FIRST_NAME               SALARY       SUM
-------------------- ---------- ---------
Jack                       8400     21600
Kimberely                  7000     21600
Charles                    6200     21600
Winston                    3200      8800
Jean                       3100      8800
Martha                     2500      8800
Girard                     2800      7000
Nandita                    4200      7000

这是SUM列包含3行的工资总和,依此类推。最后,如果保留1行或仅2行,则SUM列应包含这些1或2值本身的总和,如图所示

2 个答案:

答案 0 :(得分:2)

以下是执行此操作的几种方法:

with employees as (select 'Jack' first_name, 8700 salary from dual union all
                   select 'Kimberely' first_name, 7000 salary from dual union all
                   select 'Charles' first_name, 6200 salary from dual union all
                   select 'Winston' first_name, 3200 salary from dual union all
                   select 'Jean' first_name, 3100 salary from dual union all
                   select 'Martha' first_name, 2500 salary from dual union all
                   select 'Girard' first_name, 2800 salary from dual union all
                   select 'Nandita' first_name, 4200 salary from dual)
-- end of setting up a subquery mimicking your employees table; see SQL below:
select first_name,
       salary,
       sum(salary) over (partition by grp1) sum_salary_grps1,
       sum(salary) over (partition by grp2) sum_salary_grps2
from   (select first_name,
               salary,
               ceil((row_number() over (order by salary desc))/3) grp1,
               ceil(rownum/3) grp2
        from   employees);


FIRST_NAME     SALARY SUM_SALARY_GRPS1 SUM_SALARY_GRPS2
---------- ---------- ---------------- ----------------
Jack             8700            21900            21900
Kimberely        7000            21900            21900
Charles          6200            21900            21900
Nandita          4200            10500             7000
Winston          3200            10500             8800
Jean             3100            10500             8800
Girard           2800             5300             7000
Martha           2500             5300             8800

您注意到,当使用row_number()分析函数生成组号时,我按工资降序排序行,而获取组号的rownum方法没有固有排序。

首先,我会使用row_number()来生成组号,但是YMMV。

答案 1 :(得分:1)

堆组织表中的行不是任何顺序,这是标准的 Oracle表。因此,当你说你想要分组3行时,更大的问题是哪3行,按什么顺序排列?

您需要确保有一个属性来对行进行分组,而不会选择任何3个随机行。

让我们看一下标准SCOTT.EMP表中的一个例子。我会为每个DEPARTMENT计算 SUM

SQL> SELECT ename, SUM(sal) over(partition BY deptno) sum_sal FROM emp;

ENAME         SUM_SAL
---------- ----------
CLARK            8750
KING             8750
MILLER           8750
JONES           10875
FORD            10875
ADAMS           10875
SMITH           10875
SCOTT           10875
WARD             9400
TURNER           9400
ALLEN            9400
JAMES            9400
BLAKE            9400
MARTIN           9400

14 rows selected.