Potresti usare un case
espressione basata su @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:
Per rispondere alla domanda modificata, puoi utilizzare la stessa tecnica con count
invece di sum
:
SELECT ProductCode,
COUNT (CASE WHEN MONTH(EntryDate) = @Month THEN Quantity ELSE NULL END)
AS MonthCount,
COUNT (*) AS TotalConount
FROM ProductMaster
GROUP BY ProductCode