2011-11-23 111 views
0

經過與BIDS努力完成此操作後,我的問題在這裏:ssrs固定矩陣寬度

在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

有兩種方法可以做到這一點:

做一個直角框架,在查詢中使用不同的行和列分組值。

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