1

现在我有一个查询要我这个;

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

看不到对我的数据透视 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)
4

2 回答 2

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 演示。

于 2013-02-19T14:29:32.403 回答
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;
于 2021-05-26T19:50:42.037 回答