我需要計算indebtedness
列的值,以便當openingBalance !=0 then indebtedness = openingBalnce+SalesTotal-SalesReturn
。但是,當openingBalnce = 0 then indebtedness = indebtedness
的前monthSales
與SalesID
相同。如果previous value = 0
得到previous value
並繼續得到previous value
,直到此列有值:遞歸Sql查詢2008
SalesMonth SalesID openingBalance SalesTotal SalesReturn Indebtednes
1 1 352200 0 5600 NULL
1 2 50000 1100 0 NULL
1 3 9500 6000 0 NULL
2 1 0 0 1200 NULL
2 2 0 300 0 NULL
2 3 0 500 1000 NULL
3 1 0 600 0 NULL
3 2 0 200 0 NULL
3 3 0 0 10 NULL
.
.
.
12 1 0 0 0 NULL
12 2 0 0 0 NULL
12 3 0 0 0 NULL
與輸出是這樣的:
when openingBalance !=0 then Indebtednes=openingBalnce+SalesTotal-SalesReturn
when openingBalnce =0 then Indebtednes=Indebtednes (of the previous
month of the same SalesID)+SalesTotal-SalesReturn.
And this is the output i want.
SalesMonth SalesID openingBalance SalesTotal SalesReturn Indebtednes
---------- ------- -------------- ---------- ----------- ------------
1 1 352200 0 5600 346600
------------------------------------------------------------------------
1 2 50000 1100 0 51100
------------------------------------------------------------------------
1 3 9500 6000 0 15500
------------------------------------------------------------------------
2 1 0 0 1200 345400
------------------------------------------------------------------------
2 2 0 300 0 51400
------------------------------------------------------------------------
2 3 0 500 1000 15000
------------------------------------------------------------------------
3 1 0 600 0 346000
------------------------------------------------------------------------
3 2 0 200 0 51600
-----------------------------------------------------------------------
3 3 0 0 10 14990
-----------------------------------------------------------------------
.
.
.
12 1 0 0 0 NULL
----------------------------------------------------------------------
12 2 0 0 0 NULL
----------------------------------------------------------------------
12 3 0 0 0 NULL
可以添加銷售月= 1和銷售ID = 1的預期產出 – ughai
Indebtednes = 346600 –
我的意思是期望樣本輸出包含基於問題中上述樣本輸入的數據。您可以編輯問題併爲salesMonth = 1和SalesID = 1添加詳細信息 – ughai