这是我的桌子。 假设当前月份是6月。上个月是5月。如果当前和前几个月都存在余额,则填写当月的余额。如果当前月份不存在余额,则填充上个月的值。
输入表。
Transaction_Number Status_Indicator Balance Amount Month
20 Y 200 June
20 Y 500 May
21 Y 600 June
21 Y 700 May
22 Y 800 June
23 Y 900 May
输出:
Transaction_Number Balance_Amount
20 200
21 600
22 800
23 900
感谢您的任何帮助!
答案 0 :(得分:0)
注意:这不是经过测试的代码......现在我没有控制中心。
select currentmonth.Transaction_Number,(currentmonth.data1+previousmonth.data2)
from
(select Transaction_Number, sum(Balance Amount) data2
from <<table name>>
where month=MONTH(current date)-1
group by Transaction_Number) previousmonth
Left join
(select Transaction_Number, sum(Balance Amount) data1
from <<table name>>
where month=MONTH(current date)
group by Transaction_Number) currentmonth
on previousmonth.Transaction_Number = currentmonth.Transaction_Number
group by Transaction_Number
让我知道它是怎么回事