0

I have a table with two cols like as follows below

ID  SAL
1   1000
2   2000
3   3000
4   4000
5   5000



I'd like to have an output like that:

ID  SAL
1   1000
2   3000
3   6000
4   10000
5   15000

*/ How can we achieve this without using case statements ?

nirazul
  • 3,928
  • 4
  • 26
  • 46
bharath
  • 7
  • 2

1 Answers1

0
SELECT ID, SUM(SAL) OVER(ORDER BY ID) AS SAL
FROM YourTable;

That's called running sum and works in SQL Server 2012+

An example for response:

DECLARE @Test TABLE (ID INT, SAL INT);

INSERT INTO @Test
    (ID, SAL)
VALUES
      (1, 1000)
    , (2, 2000)
    , (3, 3000)
    , (4, 4000)
    , (5, 5000);

SELECT ID, SUM(SAL) OVER (ORDER BY ID) AS SAL
FROM @Test

Resulted in:

ID  SAL
----------
1   1000
2   3000
3   6000
4   10000
5   15000
Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
  • Hi @Evaldas Buinauskas, Hope you doing grate, thanks for your replay, But for your query the out put as follows below 1 15000.00 2 15000.00 3 15000.00 4 15000.00 5 15000.00 once again go through the scenario, and get back with proper Query. – bharath Mar 30 '15 at 12:44
  • @bharath I've just tried that by myself (see updated answer). I got correct and expected result. – Evaldas Buinauskas Mar 30 '15 at 12:50