我想弄清楚如何通過查詢得到以下結果,但無法做到這一點。按組數和總數
表名:ProductMaster
ProductCode Quantity EntryDate
A1 10 10/03/2015
A1 10 15/03/2015
A2 10 18/03/2015
A2 10 25/03/2015
A1 10 10/04/2015
A2 10 15/04/2015
我想結果爲 如果我選擇3月份當月,結果應爲:
ProductCode MonthCount TotalCount
A1 20 30
A2 20 30
如果我選擇月一個月,結果應爲:
ProductCode MonthCount TotalCount
A1 10 30
A2 10 30
我的查詢:
SELECT ProductCode, SUM(Quantity)
FROM ProductMaster
WHERE DATEPART(MONTH, EntryDate) = @Month
GROUP BY ProductCode
其中@month = 3
或4
,基於輸入。
此外,我怎樣才能得到產品代碼的數量。 對於Month = 3
ProductCode MonthCount TotalCount
A1 2 3
A2 2 3
不應該在你的第一個例子總數爲40?你的第二個例子是20? – paqogomez
總數也按產品代碼分組。因此,如果您檢查A1的總計數是30. –
EntryDate列是DateTime嗎?您嘗試過哪些查詢以獲得期望的結果? –