0

我需要获取此视图以获取报告。

交易类型 金额 (£) 贷记产品 1 (£) 产品 2 (£) 产品 3 (£)
信用卡 2489.58 829.86 739.02 920.70
借记卡 2314.93 1157.47 987.34 170.13
支票/P 订单 11266.38 3755.46 4068.92 3442.00
信用卡 - 网站 1974.21 984.93 474.21 515.07
总计 18045.10 6727.72 6269.49 5047.90

目前我有这个,几乎在那里......

交易类型 AMOUNT (£) CREDIT 产品类型
银行转账 653.89 产品类型 1
卡 1323.24 产品类型 1
现金 4538.12 产品类型 1
直接借记 118.80 产品类型 1
银行转账 653.89 产品类型 2
卡 1323.24 产品类型 2
现金 4538.12 产品类型 2
直接借记 118.80 产品类型 2

我需要在查询中更改什么?

SELECT PT.description          AS [Transaction Type], 
       Sum (P.original_amount) AS [AMOUNT (£) CREDIT], 
       PR.[description]        AS [Product Type] 
FROM   [X].[dbo].[payment] P 
       JOIN [X].[dbo].[table1] PT 
         ON P.payment_id = PT.payment_type_id 
       JOIN [X].[dbo].[table2] SO 
         ON SO.payment_type_id = P.payment_type_id 
       JOIN [X].[dbo].[table3] OI 
         ON OI.order_id = SO.site_order_id 
       JOIN [X].[dbo].[table4] PR 
         ON PR.product_id = OI.product_id 
GROUP  BY PT.description, 
          PR.description 
4

1 回答 1

2

这应该这样做:

SELECT PT.[DESCRIPTION] as [Transaction Type],
       SUM(P.ORIGINAL_AMOUNT) as [AMOUNT (£) CREDIT],
       SUM( CASE WHEN PR.[Description] = 'Product type 1' 
            THEN P.ORIGINAL_AMOUNT END) [Product 1 (£)],
       SUM( CASE WHEN PR.[Description] = 'Product type 2' 
            THEN P.ORIGINAL_AMOUNT END) [Product 2 (£)],
       SUM( CASE WHEN PR.[Description] = 'Product type 3' 
            THEN P.ORIGINAL_AMOUNT END) [Product 3 (£)]
FROM [X].[dbo].[PAYMENT] P
join [X].[dbo].[TABLE1] PT on P.PAYMENT_ID=PT.PAYMENT_TYPE_ID
join [X].[dbo].[TABLE2] SO on SO.PAYMENT_TYPE_ID=P.PAYMENT_TYPE_ID
join [X].[dbo].[TABLE3] OI on OI.ORDER_ID=SO.SITE_ORDER_ID
join [X].[dbo].[TABLE4] PR on PR.Product_id=OI.PRODUCT_ID
GROUP BY PT.[DESCRIPTION]

更新 好吧,既然您需要动态数量的列,您将需要动态 SQL(如 njk 所说)。PIVOT尽管需要 SQL Server 2005+,但我将发布一种使用.

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

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

SET @Query = '
;WITH CTE AS(
    SELECT PT.[description]        AS [Transaction Type], 
           Sum (P.original_amount) AS [AMOUNT (£) CREDIT], 
           PR.[description]        AS [Product Type] 
    FROM   [X].[dbo].[payment] P 
           JOIN [X].[dbo].[table1] PT 
             ON P.payment_id = PT.payment_type_id 
           JOIN [X].[dbo].[table2] SO 
             ON SO.payment_type_id = P.payment_type_id 
           JOIN [X].[dbo].[table3] OI 
             ON OI.order_id = SO.site_order_id 
           JOIN [X].[dbo].[table4] 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(@Query)
于 2013-02-18T16:25:46.487 回答