Request to summarize previous values

I have a table structure as shown actual data and the result expected.

I need to get the values ​​added to the sum of the previous values ​​(shown in REQUIRED RESULT) I tried the following query

SELECT empid,
sum(tot_hours) OVER (PARTITION BY empid ORDER BY empid ) AS tot_hours
        FROM empDet
       ORDER BY empid

      

But I am getting the following result set

the result for the given query

But I want a result like in the first picture.

Can anyone help me to do this?

+3


source to share


1 answer


sum (tot_hours) OVER (PARTITION by empid ORDER BY empid) AS tot_hours

Unfaithful . If you want to run SUM on then you must order tot_hours. ORDER BY

TOT_HOURS

For example, the following query will calculate the current salary of employees in each department:



SQL> SELECT deptno,
  2    sal,
  3    SUM(sal) OVER (PARTITION BY deptno ORDER BY sal ) AS tot_sal
  4  FROM emp
  5  ORDER BY deptno;

    DEPTNO        SAL    TOT_SAL
---------- ---------- ----------
        10       1300       1300
        10       2450       3750
        10       5000       8750
        20        800        800
        20       1100       1900
        20       2975       4875
        20       3000      10875
        20       3000      10875
        30        950        950
        30       1250       3450
        30       1250       3450
        30       1500       4950
        30       1600       6550
        30       2850       9400

14 rows selected.

SQL>

      

Refresh . For duplicate values, the total will be duplicated. To make it unique, use the UNLIMITED TERMINATION offer . For example,

SQL> SELECT empno, deptno,
  2    sal,
  3    SUM(sal) OVER (PARTITION BY deptno ORDER BY sal ROWS UNBOUNDED PRECEDING) AS tot_sal
  4  FROM emp
  5  ORDER BY deptno;

     EMPNO     DEPTNO        SAL    TOT_SAL
---------- ---------- ---------- ----------
      7934         10       1300       1300
                   10       1300       2600
      7782         10       2450       5050
      7839         10       5000      10050
      7369         20        800        800
      7876         20       1100       1900
      7566         20       2975       4875
      7788         20       3000       7875
      7902         20       3000      10875
      7900         30        950        950
      7521         30       1250       2200
      7654         30       1250       3450
      7844         30       1500       4950
      7499         30       1600       6550
      7698         30       2850       9400

15 rows selected.

SQL>

      

+4


source







All Articles