Du kan bruge en case
udtryk baseret på @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:
For at besvare det redigerede spørgsmål kan du bruge den samme teknik med count
i stedet for sum
:
SELECT ProductCode,
COUNT (CASE WHEN MONTH(EntryDate) = @Month THEN Quantity ELSE NULL END)
AS MonthCount,
COUNT (*) AS TotalConount
FROM ProductMaster
GROUP BY ProductCode