I'am trying to do a running total (without success) over a calculated column on sql.
My original code:
select t.codigo, t.nome, t.total
from(
select codigo, nome, SUM(CASE When ANO = 2018 Then VLCOMPRA Else 0 End ) as total
from clientes
left join ACUMTERC on ACUMTERC.TPTERC = 2 and TPOPER = 2 AND ACUMTERC.TERCEIRO = CLIENTES.CODIGO
group by codigo, nome) as t
ORDER BY total DESC
What i tried:
select t.codigo, t.nome, t.total, SUM(t.total) OVER(PARTITION BY t.codigo ORDER BY t.codigo) RunningTotal
from( select codigo, nome, SUM(CASE When ANO = 2018 Then VLCOMPRA Else 0 End ) as total from clientes left join ACUMTERC on ACUMTERC.TPTERC = 2 and TPOPER = 2 AND ACUMTERC.TERCEIRO = CLIENTES.CODIGO group by codigo, nome) as t
ORDER BY total DESC
My result:
codigo | nome | total | Running total
-------+-------+-------+---------------
000001 | name1 | 300 ! 300
000003 | name3 | 200 | 200
000002 | name2 | 100 | 100
What i need:
codigo | nome | total | Running total
-------+-------+-------+---------------
000001 | name1 | 300 ! 300
000003 | name3 | 200 | 500
000002 | name2 | 100 | 600
Any help? Thank you.