透视sql与总计

时间:2013-02-19 14:10:53

标签: sql sql-server pivot

现在我有一个问题要告诉我这个;

Transaction Type    Product 1 (£)   Product 2 (£)   Product 3 (£)


Credit Card 1   739.02  920.70
Debit Card  3   987.34  170.13
Cheques / P Orders  7   4068.92 3442.00
Credit Card - Web   1   474.21  515.07

但是现在我需要最后一行总计。

GRAND TOTAL 12  6269.49 5047.90

看不到我的pivot sql使用汇总?我可以用什么想法来获得最后一行?

DECLARE @ProductTypes AS NVARCHAR(MAX), @Query1  AS NVARCHAR(MAX);

SELECT @ProductTypes = STUFF((SELECT DISTINCT ',' + QUOTENAME([Description]) 
                              FROM [X].[dbo].[PRODUCT] 
                              FOR XML PATH(''), 
                              TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')


SET @Query1 = '

;WITH CTE AS
(

    SELECT PT.[description]       AS [Transaction Type], 
           Sum (P.original_amount) AS [AMOUNT (£) CREDIT], 
           PR.[description]        AS [Product Type] 
    FROM    [X].[dbo].[Table1] P
join [X].[dbo].[Table2] PT on P.PAYMENT_ID=PT.PAYMENT_TYPE_ID
join [X].[dbo].[Table3] SO on SO.PAYMENT_TYPE_ID=P.PAYMENT_TYPE_ID
join [X].[dbo].[Table4] OI on OI.ORDER_ID=SO.SITE_ORDER_ID
join [C].[dbo].Table5] PR on PR.Product_id=OI.PRODUCT_ID
group by PT.DESCRIPTION, PR.DESCRIPTION
)
SELECT *
FROM CTE AS T
PIVOT(SUM([AMOUNT (£) CREDIT]) FOR [Product Type] IN ('+@ProductTypes+')) AS PT
'


EXEC(@Query1)

2 个答案:

答案 0 :(得分:4)

您可以使用ROLLUP来获得所需的结果,只需稍微改变您的代码即可。

您的代码与此类似:

DECLARE @ProductTypes AS NVARCHAR(MAX), 
    @ProductTypesSum AS NVARCHAR(MAX),
    @Query1  AS NVARCHAR(MAX);

SELECT @ProductTypes = STUFF((SELECT DISTINCT ',' + QUOTENAME([Description]) 
                              FROM [X].[dbo].[PRODUCT] 
                              FOR XML PATH(''), 
                              TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

SELECT @ProductTypesSum = STUFF((SELECT DISTINCT ', Sum(' + QUOTENAME([Description])+') as '+QUOTENAME([Description])
                              FROM [X].[dbo].[PRODUCT] 
                              FOR XML PATH(''), 
                              TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')


SET @Query1 = '

;WITH CTE AS
(
    SELECT PT.[description]       AS [Transaction Type], 
           Sum (P.original_amount) AS [AMOUNT (£) CREDIT], 
           PR.[description]        AS [Product Type] 
    FROM    [X].[dbo].[Table1] P
    join [X].[dbo].[Table2] PT on P.PAYMENT_ID=PT.PAYMENT_TYPE_ID
    join [X].[dbo].[Table3] SO on SO.PAYMENT_TYPE_ID=P.PAYMENT_TYPE_ID
    join [X].[dbo].[Table4] OI on OI.ORDER_ID=SO.SITE_ORDER_ID
    join [C].[dbo].Table5] PR on PR.Product_id=OI.PRODUCT_ID
    group by PT.DESCRIPTION, PR.DESCRIPTION
)
SELECT 
    case 
        when [Transaction Type] is not null 
        then [Transaction Type] 
        else ''Grand Total'' end as [Transaction Type],
    '+@ProductTypesSum+'
FROM CTE AS T
PIVOT
(
    SUM([AMOUNT (£) CREDIT]) 
    FOR [Product Type] IN ('+@ProductTypes+')
) AS PT
group by [Transaction Type] with rollup'


EXEC(@Query1)

请参阅SQL Fiddle with Demo并修改代码。

答案 1 :(得分:0)

您可以为透视列中的所有值动态透视

DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)

select @cols = (SELECT string_agg ( QUOTENAME(PivotColumnName) ,',')

                   From   
                    (
                      select distinct(PivotColumnName)
                      from yourtable
                    )
                        )

 set @query = N'SELECT * from 
         (
            select value, PivotColumnName
            from yourtable
        ) x
        pivot 
        (
            max(value)
            for PivotColumnName in (' + @cols + N')
        ) p '

            exec sp_executesql @query;