在与BIDS挣扎以实现这一目标后,我的问题在这里:
在SSRS 2008中,我在选定月份使用带有列组的矩阵。第一列是第一个选定的月份,也是新表启动后的十二个月:
Month | Mar11 May11 Apr11 Jun11 Jul11 Aug11 Sep11 Oct11 Nov11 Dec11 Jan12 Feb12
------+------------------------------------------------------------------------
Cat.1 | 3 4 5 7 8 9 1 3
Cat.2 | 4 2 3 6 1 3 2 5
Month | Mar12 May12 Apr12
------+------------------------------------------------------------------------
Cat.1 | 3 2 1
Cat.2 | 4 1 7
这给出了一个整洁的表格,当表格“重复”(用大行组模拟)时,表格整齐地显示在整个宽度上。但是,当只选择一个月时,布局太窄,看起来很蹩脚:
Month | Mar
------+----
Cat.1 | 3
如何创建具有固定宽度(即12列宽度)的矩阵,而忽略选择了多少个月?
答案 0 :(得分:0)
有两种方法可以做到这一点:
使用不同的行和列分组值在查询中创建笛卡尔框架。
SELECT
C.MonthStarting
,R.Category
,D.InstanceCount
FROM ( -- column values
SELECT DISTINCT MonthStarting
FROM Calendar
WHERE MonthStarting BETWEEN @StartDate AND @EndDate
) AS AS C
JOIN ( -- row values
SELECT DISTINCT Category
FROM SomeRelevantTables
) AS R
ON 1 = 1 -- intentional cartesian product
LEFT JOIN (
SELECT
Category
,MonthStarting
,COUNT(1) AS InstanceCount
FROM SomeRelevantTables
GROUP BY
Category
,MonthStarting
) AS D
ON C.MonthStarting = D.MonthStarting
AND R.Category = D.Category
或者您可以在查询中转动列而不是布局,并在表格而不是矩阵中显示数据。
SELECT
T.Category
,SUM(CASE WHEN C.RowNumberAsc = 1 THEN 1 END) AS Column1
,SUM(CASE WHEN C.RowNumberAsc = 2 THEN 1 END) AS Column2
,SUM(CASE WHEN C.RowNumberAsc = 3 THEN 1 END) AS Column3
,SUM(CASE WHEN C.RowNumberAsc = 4 THEN 1 END) AS Column4
FROM SomeRelevantTables AS T
JOIN (
SELECT
C.*
,ROW_NUMBER()OVER(PARTITION BY MonthStarting) AS RowNumberAsc
FROM (
SELECT DISTINCT MonthStarting
FROM Calendar
WHERE MonthStarting BETWEEN @StartDate AND @EndDate
) AS C
) AS C
ON T.MonthStarting = C.MonthStarting
GROUP BY
T.Category