1

我有一个数据透视语句,它产生我需要的东西,除了我想在底部添加一个“总”行。任何人都可以解释一下吗?

SELECT TheProvider AS [PC],
    ISNULL([201204],0) AS [201204],
    ISNULL([201205],0) AS [201205], -- 2012/13
    ISNULL([201206],0) AS [201206], -- 2012/13
    ISNULL([201207],0) AS [201207], -- 2012/13
    ISNULL([201208],0) AS [201208], -- 2012/13
    ISNULL([201209],0) AS [201209], -- 2012/13
    ISNULL([201210],0) AS [201210], -- 2012/13
    ISNULL([201211],0) AS [201211], -- 2012/13
    ISNULL([201212],0) AS [201212], -- 2012/13
    ISNULL([201301],0) AS [201301], -- 2012/13
    ISNULL([201302],0) AS [201302], -- 2012/13
    ISNULL([201303],0) AS [201303], -- 2012/13

    ISNULL([201304],0) AS [201304], -- 2013/14
    ISNULL([201305],0) AS [201305], -- 2013/14
    ISNULL([201306],0) AS [201306], -- 2013/14
    ISNULL([201307],0) AS [201307], -- 2013/14
    ISNULL([201308],0) AS [201308], -- 2013/14
    ISNULL([201309],0) AS [201309], -- 2013/14
    ISNULL([201310],0) AS [201310], -- 2013/14
    ISNULL([201311],0) AS [201311], -- 2013/14
    ISNULL([201312],0) AS [201312], -- 2013/14
    ISNULL([201401],0) AS [201401], -- 2013/14
    ISNULL([201402],0) AS [201402], -- 2013/14
    ISNULL([201403],0) AS [201403]  -- 2013/14


FROM (
    /* START THE MAIN SELECT */
    SELECT CASE 
            WHEN ([OC (CoP)] = '5qh')
                THEN 'R1J'
            WHEN ([OC (CoP)] = 'NTY48')
                THEN 'NTY48'
            ELSE LEFT([OC (CoP)], 3)
            END AS TheProvider
        ,LEFT([Arrival Date], 6) AS TheDate
        ,COUNT(*) AS TheAmount
    FROM source.FAS WITH (NOLOCK)
    WHERE ([OC (CoP)] = 'nty48')
        OR (
            LEFT([OC (CoP)], 3) IN (
                'rte','r1j','rvj','ra7','rn3','rth','rd1','rlq','rwp','rjc','rr1','rbb'
                )
            )
    GROUP BY CASE 
            WHEN ([OC (CoP)] = '5qh')
                THEN 'R1J'
            WHEN ([OC (CoP)] = 'NTY48')
                THEN 'NTY48'
            ELSE LEFT([OC (CoP)], 3)
            END
        ,LEFT([OC (CoC)], 3)
        ,LEFT([Arrival Date], 6)
    HAVING (LEFT([Arrival Date], 6) > '201203')
        OR (LEFT([Arrival Date], 6) > '201203')
        /* END THE MAIN SELECT */
    ) UP
PIVOT(Sum(TheAmount) FOR TheDate IN (
             [201204],[201205],[201206],[201207],[201208],[201209],[201210],[201211],[201212],[201301],[201302],[201303] -- 2012/13
            ,[201304],[201305],[201306],[201307],[201308],[201309],[201310],[201311],[201312],[201401],[201402],[201403] -- 2013/14
            )) AS pvt

谢谢,

JJ

4

1 回答 1

2

您应该能够使用ROLLUP添加总计行。这会将您的代码更改为以下内容:

SELECT coalesce(TheProvider, 'Total') AS [PC],
    sum(ISNULL([201204],0)) AS [201204],
    sum(ISNULL([201205],0)) AS [201205], -- 2012/13
    sum(ISNULL([201206],0)) AS [201206], -- 2012/13
    sum(ISNULL([201207],0)) AS [201207], -- 2012/13
    sum(ISNULL([201208],0)) AS [201208], -- 2012/13
    sum(ISNULL([201209],0)) AS [201209], -- 2012/13
    sum(ISNULL([201210],0)) AS [201210], -- 2012/13
    sum(ISNULL([201211],0)) AS [201211], -- 2012/13
    sum(ISNULL([201212],0)) AS [201212], -- 2012/13
    sum(ISNULL([201301],0)) AS [201301], -- 2012/13
    sum(ISNULL([201302],0)) AS [201302], -- 2012/13
    sum(ISNULL([201303],0)) AS [201303], -- 2012/13

    sum(ISNULL([201304],0)) AS [201304], -- 2013/14
    sum(ISNULL([201305],0)) AS [201305], -- 2013/14
    sum(ISNULL([201306],0)) AS [201306], -- 2013/14
    sum(ISNULL([201307],0)) AS [201307], -- 2013/14
    sum(ISNULL([201308],0)) AS [201308], -- 2013/14
    sum(ISNULL([201309],0)) AS [201309], -- 2013/14
    sum(ISNULL([201310],0)) AS [201310], -- 2013/14
    sum(ISNULL([201311],0)) AS [201311], -- 2013/14
    sum(ISNULL([201312],0)) AS [201312], -- 2013/14
    sum(ISNULL([201401],0)) AS [201401], -- 2013/14
    sum(ISNULL([201402],0)) AS [201402], -- 2013/14
    sum(ISNULL([201403],0)) AS [201403]  -- 2013/14
FROM 
(
    /* START THE MAIN SELECT */
    SELECT CASE 
            WHEN ([OC (CoP)] = '5qh')
                THEN 'R1J'
            WHEN ([OC (CoP)] = 'NTY48')
                THEN 'NTY48'
            ELSE LEFT([OC (CoP)], 3)
            END AS TheProvider
        ,LEFT([Arrival Date], 6) AS TheDate
        ,COUNT(*) AS TheAmount
    FROM source.FAS WITH (NOLOCK)
    WHERE ([OC (CoP)] = 'nty48')
        OR (
            LEFT([OC (CoP)], 3) IN (
                'rte','r1j','rvj','ra7','rn3','rth','rd1','rlq','rwp','rjc','rr1','rbb'
                )
            )
    GROUP BY CASE 
            WHEN ([OC (CoP)] = '5qh')
                THEN 'R1J'
            WHEN ([OC (CoP)] = 'NTY48')
                THEN 'NTY48'
            ELSE LEFT([OC (CoP)], 3)
            END
        ,LEFT([OC (CoC)], 3)
        ,LEFT([Arrival Date], 6)
    HAVING (LEFT([Arrival Date], 6) > '201203')
        OR (LEFT([Arrival Date], 6) > '201203')
        /* END THE MAIN SELECT */
) UP
PIVOT(Sum(TheAmount) FOR TheDate IN (
             [201204],[201205],[201206],[201207],[201208],[201209],[201210],[201211],[201212],[201301],[201302],[201303] -- 2012/13
            ,[201304],[201305],[201306],[201307],[201308],[201309],[201310],[201311],[201312],[201401],[201402],[201403] -- 2013/14
            )
) AS pvt
group by TheProvider with rollup
于 2013-08-12T16:48:54.367 回答