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.
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.
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
Set @count:=0;
Select x,@count:=@count+y from tablename ORDER BY x;
Try above query.
Hope this will help you.