Skip to content
Advertisement

Windowing functions cumulative aggregation

I am calculating cumulative by summing some columns. The code is working. But I want to include an extra variable for the first line only. Then it miscalculates.

What I want to do in line 1:

What I want to do except line 1:

When I do this with case, the wrong result is obtained.

SQL code:

So for example on line 1, it will do this:

In places other than line 1, it will do this:

enter image description here

@devir = 10 operation on the first line:

enter image description here

Operations on the other line:

Operations of variables and columns in the first row, The sum of the columns in the row 1 and the column in the “cumulative total” in the next row

Advertisement

Answer

use ROW_NUMBER() on inner query. On outer query check for when ROW_NUMBER() = 1 include @devir in calculation

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement