Podrías usar un case
expresión basada en @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
EDITAR:
Para responder la pregunta editada, puede usar la misma técnica con count
en lugar de sum
:
SELECT ProductCode,
COUNT (CASE WHEN MONTH(EntryDate) = @Month THEN Quantity ELSE NULL END)
AS MonthCount,
COUNT (*) AS TotalConount
FROM ProductMaster
GROUP BY ProductCode