我有這個查詢可以找出每年每月的物品數量。但我期待爲累計計算結果,優化查詢針對累計結果優化查詢
SELECT
COUNT(ITM.ID) AS ItemCount,
Month(ITM.ItemProcureDate),
Year(ITM.ItemProcureDate)
FROM
Rpt_Item ITM
WHERE
ITM.ItemProcureDate IS NOT NULL
AND
ITM.ItemStatusID = 2 -- Item sold, Item Rejected
AND
ITM.ItemProcureDate >= CONVERT(DATETIME,'02/01/2014',1) [email protected]_Date
AND
ITM.ItemProcureDate <= CONVERT(DATETIME,'04/12/2014',1) [email protected]_Date
GROUP BY
Month(ITM.ItemProcureDate),
Year(ITM.ItemProcureDate)
查詢結果應該是這樣的:
Item sold In month 2
Item Sold Till Month 2
Item Rejected 1
Item Rejected Till Month 1
Year 2014
Month Feb
Last Date of Month 02/28/2014
-----------------------------------------------
Item sold In month 2
Item Sold Till Month 4
Item Rejected 1
Item Rejected Till Month 2
Year 2014
Month March
LastDate of Month 03/31/2014
-----------------------------------------------
Item sold In month 2
Item Sold Till Month 6
Item Rejected 1
Item Rejected Till Month 3
Year 2014
Month April
Last Date of Month 04/30/2014
我必須找出Item_Sold,Item_Rejected,Item_Added爲過去三個月,每一個下個月它應該是累積的所有前幾個月的Item_Sold,Item_Rejected,Item_Added
您的查詢沒有意義。它使用不在'from'子句中的表別名,如'RSK'和'IA'。 –
銷售/拒絕的邏輯是什麼?總之,如果行和列將被交換,每月一行,表示層留給程序或報表 – Serpiton
您想要爲您生成結果的查詢 未優化主版本查詢 它是不同的一個 –