fsv_whdtwiseporductstock
表
Prodcode In_stock DOC_DT
C-AW1104HB47300 10 01/08/2015
C-AW1104HB47300 40 01/08/2015
C-AW1104HB47300 50 01/08/2015
C-AW1104HB47300 10 02/08/2015
C-AW1104HB47300 20 04/08/2015
C-AW1104HB47300 10 07/08/2015
C-AW1104HB47300 5 07/08/2015
期望输出
Prodcode Doc_dt Cumulative
C-AW1104HB47300 01/08/2015 100
C-AW1104HB47300 02/08/2015 110
C-AW1104HB47300 03/08/2015 110
C-AW1104HB47300 04/08/2015 130
C-AW1104HB47300 05/08/2015 130
C-AW1104HB47300 06/08/2015 130
C-AW1104HB47300 07/08/2015 145
我做了什么
;WITH cte AS (
SELECT Row_number() OVER (ORDER BY PRODCODE, [DOC_DT]) AS rownum,*
FROM FSV_WHDTWISEPRODUCTSTOCK
)
SELECT a.PRODCODE, a.DOC_DT, a.IN_STOCK, SUM(b.IN_STOCK) AS Cumulative
FROM cte a
LEFT JOIN cte b ON a.PRODCODE = b.PRODCODE AND b.rownum <= a.rownum
where a.prodcode like 'C-AW1104HB47300'
GROUP BY a.PRODCODE, a.rownum, a.[DOC_DT], a.IN_STOCK
ORDER BY a.PRODCODE, a.[DOC_DT]
当前输出
Prodcode In_stock DOC_DT
C-AW1104HB47300 10 01/08/2015
C-AW1104HB47300 40 01/08/2015
C-AW1104HB47300 50 01/08/2015
C-AW1104HB47300 10 02/08/2015
C-AW1104HB47300 20 04/08/2015
C-AW1104HB47300 10 07/08/2015
C-AW1104HB47300 5 07/08/2015