जैसा कि ऊपर दिए गए उत्तरों में बताया गया है, आपको बाहरी जुड़ाव और किसी प्रकार की कैलेंडर तालिका की आवश्यकता होगी। यह परीक्षण नहीं किया गया है, लेकिन मुझे लगता है कि यह आपके लिए काम करेगा:
with dateCTE as
(
select cast('2012-01-01' as datetime) dateValue -- start date
union all
select DateAdd(mm, 1, dateValue)
from dateCTE
where dateValue < '2012-12-30' -- end date
)
SELECT
MONTH(dateCTE.dateValue) AS 'MONTH',
COUNT(CASE WHEN @Worker_ID1 IS NULL OR @Worker_ID1 = Worker.ID THEN 1 END) AS 'Worker1',
COUNT(CASE WHEN @Worker_ID2 IS NULL OR @Worker_ID2 = Worker.ID THEN 1 END) AS 'Worker2',
COUNT(CASE WHEN @Worker_ID3 IS NULL OR @Worker_ID3 = Worker.ID THEN 1 END) AS 'Worker3',
COUNT(CASE WHEN @Worker_ID4 IS NULL OR @Worker_ID4 = Worker.ID THEN 1 END) AS 'Worker4',
COUNT(CASE WHEN @Worker_ID5 IS NULL OR @Worker_ID5 = Worker.ID THEN 1 END) AS 'Worker5'
FROM dateCTE
LEFT JOIN Ord on MONTH(dateCTE.datevalue) = MONTH(Ord.OrdDate)
JOIN Prod ON Ord.Prod_ID = Prod.ID
JOIN ProdType ON Prod.ProdType_ID = ProdType.ID
JOIN Groupord ON Ord.Groupord_ID = Groupord.ID
JOIN Worker ON Groupord.Worker_ID = Worker.ID
WHERE (@Year = YEAR(Ord.OrdDate) or ORD.prod_id is null)
AND (@DrugType_ID IS NULL OR @ProdType_ID = ProdType.ID)
GROUP BY MONTH(dateCTE.dateValue)
OPTION (MAXRECURSION 0)