Anda bisa menggunakan case
ekspresi berdasarkan @Month
:
SELECT ProductCode,
SUM (CASE WHEN MONTH(EntryDate) = @Month THEN Quantity ELSE 0 END)
AS MonthCount,
SUM (Quantity) AS TotalConount
FROM ProductMaster
GROUP BY ProductCode
EDIT:
Untuk menjawab pertanyaan yang diedit, Anda dapat menggunakan teknik yang sama dengan count
bukannya sum
:
SELECT ProductCode,
COUNT (CASE WHEN MONTH(EntryDate) = @Month THEN Quantity ELSE NULL END)
AS MonthCount,
COUNT (*) AS TotalConount
FROM ProductMaster
GROUP BY ProductCode