Query to sum the previous values

Shanna

I have the table strucure as in the imageactual data and the result expected.

I need to get the values added to the sum of previous values(shown in the REQUIRED RESULT) I tried with 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 get the following result set

the result for the given query

But I need the result as in the first picture.

Can anyone help me doing this?

Lalit Kumar B

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

Your ORDER BY is incorrect. If you want the running SUM on the TOT_HOURS, then you should order by tot_hours.

For example, the below query will calculate the running sum of 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>

Update For duplicate values, the running total would be duplicate. To make it unique, use UNBOUNDED PRECEDING clause. 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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Query to sum the previous values

From Dev

Oracle sum previous values

From Dev

Sum of the values of the previous dates in SAS

From Dev

r: sum values of previous observations

From Dev

How can sum values from the previous month?

From Dev

Get sum of previous 6 values including the group

From Dev

LINQ query to Group and SUM values

From Dev

SQL Query - Group and SUM of Values

From Dev

sum values in rows mysql query

From Dev

Add to query in order to get total sum of previous results SQL

From Dev

How to get the sum of a count of records (from a previous query)

From Dev

Sum of the values in the previous cell/s, if current cell/s is an error

From Dev

Sum every last value with all previous values in array

From Dev

Sum of values from 3rd previous month

From Dev

Gettting cumulative sum of previous values except for the first value

From Dev

Sum of the values in the previous cell/s, if current cell/s is an error

From Dev

Sum ONLY at the last row where previous rows have same values

From Dev

Find sum of max values in a single SELECT query

From Java

Creating a query containing a SUM() of MAX() values with conditions

From Dev

SQL query - sum of values by status for date interval

From Dev

SUM a list of values (contain sub query)

From Dev

Using a sum of values as a condition (SQL query)

From Dev

Sum Query with same ID values no repeating

From Dev

Handling NULL values in an SQL Query that uses SUM

From Dev

SUM query for JSON values in JSON array

From Dev

SQL Query - Getting the sum of different varchar values

From Dev

Mysql query sum values into multiple rows

From Dev

get sum of column values from SQL query

From Dev

Getting the sum of values in same column in SQL QUERY

Related Related

  1. 1

    Query to sum the previous values

  2. 2

    Oracle sum previous values

  3. 3

    Sum of the values of the previous dates in SAS

  4. 4

    r: sum values of previous observations

  5. 5

    How can sum values from the previous month?

  6. 6

    Get sum of previous 6 values including the group

  7. 7

    LINQ query to Group and SUM values

  8. 8

    SQL Query - Group and SUM of Values

  9. 9

    sum values in rows mysql query

  10. 10

    Add to query in order to get total sum of previous results SQL

  11. 11

    How to get the sum of a count of records (from a previous query)

  12. 12

    Sum of the values in the previous cell/s, if current cell/s is an error

  13. 13

    Sum every last value with all previous values in array

  14. 14

    Sum of values from 3rd previous month

  15. 15

    Gettting cumulative sum of previous values except for the first value

  16. 16

    Sum of the values in the previous cell/s, if current cell/s is an error

  17. 17

    Sum ONLY at the last row where previous rows have same values

  18. 18

    Find sum of max values in a single SELECT query

  19. 19

    Creating a query containing a SUM() of MAX() values with conditions

  20. 20

    SQL query - sum of values by status for date interval

  21. 21

    SUM a list of values (contain sub query)

  22. 22

    Using a sum of values as a condition (SQL query)

  23. 23

    Sum Query with same ID values no repeating

  24. 24

    Handling NULL values in an SQL Query that uses SUM

  25. 25

    SUM query for JSON values in JSON array

  26. 26

    SQL Query - Getting the sum of different varchar values

  27. 27

    Mysql query sum values into multiple rows

  28. 28

    get sum of column values from SQL query

  29. 29

    Getting the sum of values in same column in SQL QUERY

HotTag

Archive