2013-10-31 67 views
1

我有這個樣本數據:TSQL PIVOT具有多個透視列和未透視列?

PERIOD FINANCE TYPE SEGMENT DEPARTMENT   PERIOD VALUE ACCUMULATED VALUE 
1  Actual  HE  Business   -9394.6   -9394.6 
2  Actual  HE  Business   141646.6  132252 
3  Actual  HE  Business   145920.25  278172.25 
1  Actual  HE  Business Skills  0    0 
2  Actual  HE  Business Skills  0    0 
3  Actual  HE  Business Skills  0    0 
1  Actual SERVICES Catering Services 32359.59  32359.59 
2  Actual SERVICES Catering Services 147628.38  179987.97 
3  Actual SERVICES Catering Services 174413.87  354401.84 
1  Budget  HE  Business   139812.27  139812.27 
2  Budget  HE  Business   139812.27  279624.54 
3  Budget  HE  Business   139812.27  419436.81 
1  Budget  HE  Business Skills  0    0 
2  Budget  HE  Business Skills  0    0 
3  Budget  HE  Business Skills  0    0 
1  Budget SERVICES Catering Services 137500   137500 
2  Budget SERVICES Catering Services 137500   275000 
3  Budget SERVICES Catering Services 137500   412500 

我需要把它的格式如下:

FINANCE TYPE SEGMENT DEPARTMENT   PERIOD VALUE 1 PERIOD VALUE 2 PERIOD VALUE 3 ACCUMULATED VALUE 1 ACCUMULATED VALUE 2 ACCUMULATED VALUE 3 
Actual   HE  Business   -9394.6   141646.6  145920.25    -9394.6    132252   278172.25 
Actual   HE  Business Skills  0    0    0 0 0 0 
Actual  SERVICES Catering Services 32359.59  147628.38  174413.87 32359.59 179987.97 354401.84 
Budget   HE  Business   139812.27  139812.27  139812.27 139812.27 279624.54 419436.81 
Budget   HE  Business Skills  0    0    0 0 0 0 
Budget  SERVICES Catering Services 137500   137500   137500 137500 275000 412500 

我嘗試下面的代碼,但那裏不應該得到幾個NULL值並且輸出是錯誤的。我仍然試圖理解一般使用交叉應用和樞軸。

SELECT [FINANCE TYPE], SEGMENT, 
    DEPARTMENT, [PERIOD VALUE1], 
    [PERIOD VALUE2], [PERIOD VALUE3], 
    [PERIOD VALUE4], [PERIOD VALUE5], 
    [PERIOD VALUE6], [PERIOD VALUE7], 
    [PERIOD VALUE8], [PERIOD VALUE9], 
    [PERIOD VALUE10], [PERIOD VALUE11], 
    [PERIOD VALUE12], [ACCUMULATED VALUE1], 
    [ACCUMULATED VALUE2], [ACCUMULATED VALUE3], 
    [ACCUMULATED VALUE4], [ACCUMULATED VALUE5], 
    [ACCUMULATED VALUE6], [ACCUMULATED VALUE7], 
    [ACCUMULATED VALUE8], [ACCUMULATED VALUE9], 
    [ACCUMULATED VALUE10], [ACCUMULATED VALUE11], 
    [ACCUMULATED VALUE12] 
FROM 
( 
    SELECT PERIOD, [FINANCE TYPE], 
     SEGMENT, DEPARTMENT, 
     COL, VALUE, COL+PERIOD AS PRD 
    FROM CTE 
    CROSS APPLY 
    ( 
     SELECT 'PERIOD VALUE', [PERIOD VALUE] UNION ALL 
     SELECT 'ACCUMULATED VALUE', [ACCUMULATED VALUE] 
    )C(COL, VALUE) 
)D 
PIVOT 
( 
    MAX(VALUE) 
    FOR [PRD] IN ([PERIOD VALUE1], [PERIOD VALUE2], [PERIOD VALUE3], 
       [PERIOD VALUE4], [PERIOD VALUE5], [PERIOD VALUE6] , 
       [PERIOD VALUE7], [PERIOD VALUE8], [PERIOD VALUE9], 
       [PERIOD VALUE10], [PERIOD VALUE11], [PERIOD VALUE12] , 
       [ACCUMULATED VALUE1], [ACCUMULATED VALUE2], 
       [ACCUMULATED VALUE3], [ACCUMULATED VALUE4], 
       [ACCUMULATED VALUE5], [ACCUMULATED VALUE6] , 
       [ACCUMULATED VALUE7], [ACCUMULATED VALUE8], 
       [ACCUMULATED VALUE9], [ACCUMULATED VALUE10], 
       [ACCUMULATED VALUE11], [ACCUMULATED VALUE12]) 
)PIV 

任何幫助來幫助我解決這個問題將不勝感激。

回答

1

我發現現有代碼存在一些問題。首先,在你的子查詢中你重複了colPeriod - 一旦它被用來創建Period的新列名,並且它自己創建一次。這將改變你的結果,因爲colPeriod本身將被分組,所以如果你不在最終結果中使用它 - 不要將它包含在子查詢中。

的代碼應該修改爲以下內容:

SELECT [FINANCE TYPE], SEGMENT, 
    DEPARTMENT, [PERIOD VALUE1], 
    [PERIOD VALUE2], [PERIOD VALUE3], 
    [PERIOD VALUE4], [PERIOD VALUE5], 
    [PERIOD VALUE6], [PERIOD VALUE7], 
    [PERIOD VALUE8], [PERIOD VALUE9], 
    [PERIOD VALUE10], [PERIOD VALUE11], 
    [PERIOD VALUE12], [ACCUMULATED VALUE1], 
    [ACCUMULATED VALUE2], [ACCUMULATED VALUE3], 
    [ACCUMULATED VALUE4], [ACCUMULATED VALUE5], 
    [ACCUMULATED VALUE6], [ACCUMULATED VALUE7], 
    [ACCUMULATED VALUE8], [ACCUMULATED VALUE9], 
    [ACCUMULATED VALUE10], [ACCUMULATED VALUE11], 
    [ACCUMULATED VALUE12] 
FROM 
( 
    SELECT [FINANCE TYPE], 
     SEGMENT, DEPARTMENT, 
     VALUE, 
     COL+cast(PERIOD as varchar(10)) AS PRD 
    FROM yourtable 
    CROSS APPLY 
    ( 
     SELECT 'PERIOD VALUE', [PERIOD VALUE] UNION ALL 
     SELECT 'ACCUMULATED VALUE', [ACCUMULATED VALUE] 
    )C(COL, VALUE) 
)D 
PIVOT 
( 
    MAX(VALUE) 
    FOR [PRD] IN ([PERIOD VALUE1], [PERIOD VALUE2], [PERIOD VALUE3], 
       [PERIOD VALUE4], [PERIOD VALUE5], [PERIOD VALUE6] , 
       [PERIOD VALUE7], [PERIOD VALUE8], [PERIOD VALUE9], 
       [PERIOD VALUE10], [PERIOD VALUE11], [PERIOD VALUE12] , 
       [ACCUMULATED VALUE1], [ACCUMULATED VALUE2], 
       [ACCUMULATED VALUE3], [ACCUMULATED VALUE4], 
       [ACCUMULATED VALUE5], [ACCUMULATED VALUE6] , 
       [ACCUMULATED VALUE7], [ACCUMULATED VALUE8], 
       [ACCUMULATED VALUE9], [ACCUMULATED VALUE10], 
       [ACCUMULATED VALUE11], [ACCUMULATED VALUE12]) 
)PIV 

SQL Fiddle with Demo

+0

感謝這個......只是爲什麼會有NULLS?週期1,2,3和累積1,2,3有值,所以它們爲什麼顯示在單獨的行上而不是全部顯示在一行上。這就是我希望作爲我的輸出 –

+1

@Chen看到我的編輯,我錯過了你也必須從子查詢中刪除'Period' - 你應該有正確的結果現在看到這個演示 - http:// sqlfiddle。 com /#!3/83ea2/5 – Taryn

+0

優秀的幫助!非常感謝x –