|
|
Question : SQL Cumulative
|
|
I have a query that return this:
DATA CASOS_CRIADOS CASOS_ENCERRADOS A-B 01/03/2006 53 42 11 02/03/2006 100 55 45 03/03/2006 107 91 16 04/03/2006 89 17 72 ...
What can I create a collum named "A-B-CUMULATIVE", thats represents the cumulative values from collum CRIADOS_MINUS_ENCERRADOS? The result is this:
DATA CASOS_CRIADOS CASOS_ENCERRADOS A-B A-B-CUMULATIVE 01/03/2006 53 42 11 11 02/03/2006 100 55 45 65 <-- THE LAST A-B + ACTUAL 03/03/2006 107 91 16 81 <-- THE LAST A-B + ACTUAL 04/03/2006 89 17 72 153 <-- THE LAST A-B + ACTUAL ...
Thanks in advance,
Felipe.
|
Answer : SQL Cumulative
|
|
I think matthewspatrick's query needs some adjustments:
SELECT DATA, CASOS_CRIADOS, CASOS_ENCERRADOS, (CASOS_CRIADOS - CASOS_ENCERRADOS) AS [A-B], (SELECT SUM(CASOS_CRIADOS - CASOS_ENCERRADOS) FROM YourTable tt WHERE tt.DATA <= t.DATA) AS [A-B-Cumulative] FROM YourTable t ORDER BY DATA
|
|
|
|
|