我在 ssrs 报告中使用以下查询。
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)
这将返回一个具有正确分组的表,除了尚未发生或未记录的月份不会出现。我希望每个月都显示,即使它还没有过去,我希望他们的订单价值为零。
有什么方法可以在 FROM 子句中放置一个数组或类似的东西吗?谢谢。