1
I’m having a little problem with SQL. I need to increment a variable sel2 from a condition defined in sel1, for example:
linha | sel1 | sel2
1 | 1 | 1
2 | -1 | -1
3 | -1 | -2
4 | 1 | 1
5 | 1 | 2
6 | 1 | 3
7 | -1 | -1
so on and so forth
I tried to solve through the following code:
SELECT t1.linha ,
t1.sel1 ,
SUM(t2.sel1) sel2
FROM #A AS t1
INNER JOIN #A AS t2 ON t1.linha>= t2.linha
GROUP BY t1.linha,
t1.sel1
ORDER BY t1.linha ASC
However, the result increases cumulatively, thus:
linha | sel1 | sel2
1 | 1 | 1
2 | -1 | -1
3 | -1 | -2
4 | 1 | 2
5 | 1 | 3
6 | 1 | 4
7 | -1 | -3
The fact is that I could not get the expected result. Would anyone have any idea for the solution.
thanks