以下SQL作法,可以取出資料前6天金額加總
前6天是指有輸入的資料往前推6天
SELECT DATE,SUM(SALAMT) AS SALAMT,S.CUNO
--取出前6天之金額平均值
,(SELECT AVG(SALAMT) FROM FDRSALSI S1 WHERE S1.CUNO=S.CUNO AND DATE
IN(SELECT TOP 6 DATE FROM FDRSALSI S2 WHERE S2.CUNO=S.CUNO
AND S2.DATE <S.DATE GROUP BY DATE,CUNO ORDER BY DATE DESC)
) AS AVGAMT
FROM FDRSALSI S
WHERE DATE='20100211'
GROUP BY DATE,S.CUNO
沒有留言:
張貼留言