添加小计&总计为Pivot

时间:2017-05-10 11:45:37

标签: sql tsql

我有以下查询

SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person], 
       [Region], [Store Type], ISNULL([2016], 0) AS '2016', ISNULL([2017],0) AS 
       '2017'
FROM
(
    SELECT CUCODE AS 'Acct', CUNAME AS 'Customer', STKCODE AS 'Code', STKNAME AS 'Description', SUM(OD_QTYORD) AS 'Packs Ordered'
        , OD_GROSS AS 'Value', CUSORT AS 'Sale Person', LEFT(CUUSER1,3) AS 'Region', DATEPART(yyyy, OD_DATE) AS 'Year', OH_USER2 AS 'Store Type'
    FROM STK_STOCK
    INNER JOIN ORD_DETAIL ON STK_STOCK.STKCODE=ORD_DETAIL.OD_STOCK_CODE
    INNER JOIN ORD_HEADER ON OD_ORDER_NUMBER=OH_ORDER_NUMBER
    INNER JOIN SL_ACCOUNTS ON OH_ACCOUNT=CUCODE
    WHERE STKCODE IN ('76958', '27225', '27221', '26962', '26959', '26961', '27226', '26963', '26960')
    AND OD_QTYORD > 0
    AND CUCODE != 'Z9997'
    GROUP BY CUCODE, CUNAME, STKCODE, STKNAME, OD_GROSS, CUSORT, CUUSER1, OD_DATE, OH_USER2
) AS SourceTable
PIVOT
(
    SUM([Packs Ordered])
    FOR [Year] IN ([2016], [2017])
) AS PivotTable;

输出看起来很棒,但我想要每个'Acct'的小计和整个事件的Grand Total。

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以按组分组使用分组:

SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person], 
           [Region], [Store Type], sum([2016]) as [2016], sum([2017]) as [2017] FROM 
(
    SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person], 
           [Region], [Store Type], ISNULL([2016], 0) AS '2016', ISNULL([2017],0) AS 
           '2017'
    FROM
    (
        SELECT CUCODE AS 'Acct', CUNAME AS 'Customer', STKCODE AS 'Code', STKNAME AS 'Description', SUM(OD_QTYORD) AS 'Packs Ordered'
            , OD_GROSS AS 'Value', CUSORT AS 'Sale Person', LEFT(CUUSER1,3) AS 'Region', DATEPART(yyyy, OD_DATE) AS 'Year', OH_USER2 AS 'Store Type'
        FROM STK_STOCK
        INNER JOIN ORD_DETAIL ON STK_STOCK.STKCODE=ORD_DETAIL.OD_STOCK_CODE
        INNER JOIN ORD_HEADER ON OD_ORDER_NUMBER=OH_ORDER_NUMBER
        INNER JOIN SL_ACCOUNTS ON OH_ACCOUNT=CUCODE
        WHERE STKCODE IN ('76958', '27225', '27221', '26962', '26959', '26961', '27226', '26963', '26960')
        AND OD_QTYORD > 0
        AND CUCODE != 'Z9997'
        GROUP BY CUCODE, CUNAME, STKCODE, STKNAME, OD_GROSS, CUSORT, CUUSER1, OD_DATE, OH_USER2
    ) AS SourceTable
    PIVOT
    (
        SUM([Packs Ordered])
        FOR [Year] IN ([2016], [2017])
    ) AS PivotTable
) a
group by GROUPING SETS ( [Acct], [Customer], [Code], [Description], [Value], [Sale Person], 
           [Region], [Store Type] )
ORDER BY [Acct], [Customer], [Code], [Description], [Value], [Sale Person], 
           [Region], [Store Type];

答案 1 :(得分:0)

根据Kannan Kandasamy的回复,我得到了这个很漂亮的作品:

SELECT (CASE 
            WHEN GROUPING([Acct])=0 AND
                    GROUPING([Customer]) = 1 AND
                    GROUPING([Code]) = 1 AND
                    GROUPING([Description]) = 1 AND
                    GROUPING([Sale Person]) = 1 AND
                    GROUPING([Region]) = 1 AND
                    GROUPING([Store Type]) = 1
                    THEN 'Total '+ [Acct]
            WHEN GROUPING([Acct])=1 AND
                    GROUPING([Customer]) = 1 AND
                    GROUPING([Code]) = 1 AND
                    GROUPING([Description]) = 1 AND
                    GROUPING([Sale Person]) = 1 AND
                    GROUPING([Region]) = 1 AND
                    GROUPING([Store Type]) = 1
                    THEN 'Total'
            ELSE [Acct]
    END) AS Acct , ISNULL([Customer],'') AS 'Customer', ISNULL([Code],'') AS 'Code', ISNULL([Description],'') AS 'Description', SUM(ISNULL([Value],'')) AS 'Value', ISNULL([Sale Person],'') AS 'Sales Person', 
       ISNULL([Region],'') AS 'Region', ISNULL([Store Type],'') AS 'Store Type', SUM([2016]) AS '2016', SUM([2017]) AS '2017'
 FROM 
(
SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person], 
       [Region], [Store Type], ISNULL([2016], 0) AS '2016', ISNULL([2017],0) AS 
       '2017'
FROM
(
    SELECT CUCODE AS 'Acct', CUNAME AS 'Customer', STKCODE AS 'Code', STKNAME AS 'Description', SUM(OD_QTYORD) AS 'Packs Ordered'
        , OD_GROSS AS 'Value', CUSORT AS 'Sale Person', LEFT(CUUSER1,3) AS 'Region', DATEPART(yyyy, OD_DATE) AS 'Year', OH_USER2 AS 'Store Type'
    FROM STK_STOCK
    INNER JOIN ORD_DETAIL ON STK_STOCK.STKCODE=ORD_DETAIL.OD_STOCK_CODE
    INNER JOIN ORD_HEADER ON OD_ORDER_NUMBER=OH_ORDER_NUMBER
    INNER JOIN SL_ACCOUNTS ON OH_ACCOUNT=CUCODE
    WHERE STKCODE IN ('76958', '27225', '27221', '26962', '26959', '26961', '27226', '26963', '26960')
    AND OD_QTYORD > 0
    AND CUCODE != 'Z9997'
    GROUP BY CUCODE, CUNAME, STKCODE, STKNAME, OD_GROSS, CUSORT, CUUSER1, OD_DATE, OH_USER2
) AS SourceTable
PIVOT
(
    SUM([Packs Ordered])
    FOR [Year] IN ([2016], [2017])
) AS PivotTable
) a
group by GROUPING SETS (( [Acct], [Customer], [Code], [Description], [Sale Person], 
       [Region], [Store Type]),([Acct]),());