I am using the following query in an ssrs report.
Declare @Year int = 2012
Declare @ProdType_ID int = 1
SELECT
MONTH(Ord.OrdDate) AS 'MONTH',
COUNT(CASE WHEN @Year = YEAR(Ord.OrdDate) THEN 1 END) as Order1,
COUNT(CASE WHEN @Year-1 = YEAR(Ord.OrdDate) THEN 1 END) as 'Order2'
FROM
(
select 1 as number
union
select 2
union
select 3
union
select 4
union
select 5
union
select 6
union
select 7
union
select 8
union
select 9
union
select 10
union
select 11
union
select 12
) as months
JOIN Ord on Number = Month(Ord.OrdDate)
JOIN Prod ON Ord.Prod_ID = Prod.ID
JOIN ProdType ON Prod.ProdType_ID = ProdType.ID
WHERE @ProdType_ID = ProdType.ID
GROUP BY months.number, Month(Ord.OrdDate)
This returns a table with the correct grouping except for the fact that months that haven’t happened yet or were not recorded don’t appear. I would like every month to display even if it hasn’t come to pass yet and I would like their order values to be zero.
Is there some way I could put an array or something similar in the FROM clause?
Thanks.
It worked after removing the WHERE clause and doing all the filtering in the count aggregate.