现在我有一个查询要我这个;
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)