-1

Suppose we have a table A,

x   y

1   2
2   3
3   5
4   6
5   9
6   10

Write a query which sums up like below

table b

x   y
1   2
2   5
3   10
4   16
5   25
6   35

The above problem is to be solved without using Loops.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Chetan Kulkarni
  • 404
  • 4
  • 15

2 Answers2

2

use sum() over () clause

CREATE TABLE #Table1
    ([x] int, [y] int)
;

INSERT INTO #Table1
    ([x], [y])
VALUES
    (1, 2),
    (2, 3),
    (3, 5),
    (4, 6),
    (5, 9),
    (6, 10)
;
;
select x, sum(y) over (order by x) as y from #table1
Chanukya
  • 5,833
  • 1
  • 22
  • 36
0
Set @count:=0;
Select x,@count:=@count+y from tablename ORDER BY x;

Try above query.

Hope this will help you.

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38