计算上个月余额的当前总结转额

时间:2019-04-26 13:58:59

标签: sql

我需要一个SQL查询,该查询会将上个月的“当前余额”放入下个月的“当前总计”。附件是我预期输出的图像。请帮忙。

我研究了LAG分析功能,但无法获得所需的结果。我也尝试过使用CASE语句,但这还没有提供预期的输出。

这是我的查询:

select 
c.Client,
case
 when (a.CurrentTotal -isnull(b.Withdrawl,0)) > 0
 then a.CurrentTotal -isnull(b.Withdrawl,0)
 else a.CurrentTotal
end as CurrentTotal,
isnull(b.Withdrawl,0) as Withdrawl,  
case
 when b.Withdrawl > 0 
 then a.CurrentTotal -isnull(b.Withdrawl,0) -isnull(b.Withdrawl,0)
end as CurrentBalance

from CurrentTotalTable a 
left outer join WithdrawlTable b
    on a.ClientID = b.ClientID
inner join [dbo].ClientTable c
    on a.ClientID = c.ClientID

where b.WithdrawlDate between '10/1/2018' and '10/31/2018' [attached image][1]

0 个答案:

没有答案