2012-12-11 83 views
1

我創建了一個數據透視表用下面的代碼:添加透視表的總計

DECLARE @SQL AS VARCHAR(MAX) 
DECLARE @Columns AS VARCHAR (MAX) 

SELECT @Columns = 
COALESCE(@Columns + ', ','')+ QUOTENAME(PortfolioID) 
FROM 
(
SELECT PortfolioID FROM InfoPortal.DBO.fn_Generic_PortfolioGroupMembers (@PortfolioGroup) 
) AS B 
ORDER BY B.PortfolioID 

SET @SQL = ' 
WITH PivotData AS 
(
    SELECT 
     PortfolioID, Percentage, DurationBand, DurationSort 
    FROM #Worktable 
) 

SELECT 
    DurationBand, 
    ' + @Columns + ' 
FROM PivotData 
PIVOT 
(
    SUM(Percentage) 
    FOR PortfolioID 
    IN (' + @Columns + ') 
) AS PivotResult 
ORDER BY DurationSort' 

EXEC (@SQL) 

但是我希望做的是添加總計爲每個portfolioID,我不確定如何做到這一點。任何幫助?

+0

你想總計作爲附加行或作爲一個列? – Taryn

+0

我想要一個總的行 –

回答

3

您應該能夠添加GROUP BY with ROLLUP產生總計行,與此類似:

DECLARE @SQL AS VARCHAR(MAX) 
DECLARE @Columns AS VARCHAR (MAX) 
DECLARE @ColumnsRollup AS VARCHAR (MAX) 

SELECT @Columns = 
COALESCE(@Columns + ', ','')+ QUOTENAME(PortfolioID) 
FROM 
(
    SELECT distinct PortfolioID 
    FROM worktable 
) AS B 
ORDER BY B.PortfolioID 

SELECT @ColumnsRollup = 
COALESCE(@ColumnsRollup + ', Sum(','Sum(')+ QUOTENAME(cast(PortfolioID as varchar(10)))+') as Portfolio'+cast(PortfolioID as varchar(10)) 
FROM 
(
    SELECT distinct PortfolioID 
    FROM worktable 
) AS B 
ORDER BY B.PortfolioID 

SET @SQL = ' 
      WITH PivotData AS 
      (
       SELECT PortfolioID, Percentage, DurationBand, DurationSort 
       FROM Worktable 
      ) 
      SELECT case when DurationBand is not null then cast(durationband as varchar(10)) 
       else ''Total'' end Durationband, ' + @ColumnsRollup+ ' 
      FROM 
      (
       SELECT DurationBand, ' + @Columns + ' 
       FROM PivotData 
       PIVOT 
       (
        SUM(Percentage) 
        FOR PortfolioID IN (' + @Columns + ') 
       ) AS PivotResult 
      ) src 
      GROUP BY DurationBand with ROLLUP' 

EXEC (@SQL) 

SQL Fiddle with Demo注意:示例數據只是基於您的表結構進行了嘲弄。

結果:

| DURATIONBAND | PORTFOLIO1 | PORTFOLIO2 | PORTFOLIO3 | 
------------------------------------------------------- 
|   2 |   78 |  (null) |  (null) | 
|   5 |  (null) |  (null) |   4 | 
|   12 |   10 |   45 |  (null) | 
|  Total |   88 |   45 |   4 | 

總部設在需要保持整理東西,你可以使用:

DECLARE @SQL AS VARCHAR(MAX) 
DECLARE @Columns AS VARCHAR (MAX) 
DECLARE @ColumnsRollup AS VARCHAR (MAX) 

SELECT @Columns = 
COALESCE(@Columns + ', ','')+ QUOTENAME(PortfolioID) 
FROM 
(
    SELECT distinct PortfolioID 
    FROM worktable 
) AS B 
ORDER BY B.PortfolioID 

SELECT @ColumnsRollup = 
COALESCE(@ColumnsRollup + ', Sum(','Sum(')+ QUOTENAME(cast(PortfolioID as varchar(10)))+') as '+QUOTENAME(cast(PortfolioID as varchar(10))) 
FROM 
(
    SELECT distinct PortfolioID 
    FROM worktable 
) AS B 
ORDER BY B.PortfolioID 

SET @SQL = ' 
      WITH PivotData AS 
      (
       SELECT PortfolioID, Percentage, DurationBand, DurationSort 
       FROM Worktable 
      ) 
      SELECT * 
      FROM 
      (
       SELECT DurationBand, ' + @Columns + ' 
       FROM PivotData 
       PIVOT 
       (
        SUM(Percentage) 
        FOR PortfolioID IN (' + @Columns + ') 
       ) AS PivotResult 
       UNION ALL 
       select ''Grand Total'', '[email protected]+' 
       from 
       (
        SELECT DurationBand, ' + @Columns + ' 
        FROM PivotData 
        PIVOT 
        (
         SUM(Percentage) 
         FOR PortfolioID IN (' + @Columns + ') 
        ) AS PivotResult 
       )tot 
      ) src 
      ' 

EXEC (@SQL) 

SQL Fiddle with Demo

+0

看起來不錯,但有幾件事。我如何讓「總計」出現在持續時間欄中?我如何通過DurationSort命令來確定總數,但總數保持在底部? –

+0

@chrissyp我更新了代碼以刪除null,並用'durationband'列中的'total'替換它。我不確定你的意思是按'durationsort'命令'pivot',你可以用一些示例數據改變sql小提琴,然後解釋所需的結果嗎? – Taryn

+0

感謝編輯@bluefeet,但我仍然可以通過DurationSort進行排序。它必須像在某處添加ORDER BY DurationSort一樣簡單?基本上,持續時間排序僅僅是一個從1,2,3開始的INT ......這樣我就可以將持續時間帶刺入正確的順序。我想我已經正確更新了SQL小提琴。 –