我想上一行的值添加到當前行入列累計SQL未能從上一行增加值到下一個
Select
Ddate as Date, etype, Reference, linkacc as ContraAcc,
Description,
sum(case when amount > 0 then amount else 0 end) as Debits,
sum(case when amount < 0 then amount else 0 end) as Credits,
sum(amount) as Cumulative
from
dbo.vw_LT
where
accnumber ='8400000'
and [DDate] between '2016-04-01 00:00:00' and '2016-04-30 00:00:00'
and [DataSource] = 'PAS11CEDCRE17'
group by
Ddate, etype, Reference, linkacc, Description, Amount
輸出(什麼我得到):
Date Reference ContraAcc Description Debits Credits Cumulative
--------------------------------------------------------------------------
2016-04-01 CC007 8000000 D/CC007 0 -39.19 -39.19
2016-04-01 CC007 8000000 D/CC007 1117.09 0 1117.09
2016-04-01 CC009 8000000 CC009 2600 0 2600
的累積列
想下面(我需要):
Date Reference ContraAcc Description Debits Credits Cumulative
--------------------------------------------------------------------------
2016-04-01 CC007 8000000 D/CC007 0 -39.19 -39.19
2016-04-01 CC007 8000000 D/CC007 1117.09 0 1077.9
2016-04-01 CC009 8000000 CC009 2600 0 3677.9
你應該明白我的意思張貼一些取樣數據和預期結果,因爲你的問題不清楚。還要標記你的rdbms(oracle,mysql,sql-server),因爲答案會有很大的不同。你上面的查詢沒有機制,會知道其他行,所以他們將不會運行或累計總數 – Matt
我已經添加了示例數據 – Tinashe
我不明白什麼「在累積列第二行必須是-39.19 + 1117.09和等等「的意思 - 你能說清楚一點嗎?您輸出的內容是您目前得到的或您想要的內容? – Blorgbeard