翻转sql中的数据

时间:2013-02-18 16:14:26

标签: sql sql-server

我需要获得此报告的视图。

Transaction Type    Amount (£) Credit   Product 1 (£)   Product 2 (£)   Product 3 (£)
Credit Card         2489.58             829.86          739.02          920.70
Debit Card          2314.93             1157.47         987.34          170.13
Cheques / P Orders  11266.38            3755.46         4068.92         3442.00
Credit Card - Web   1974.21             984.93          474.21          515.07
GRAND TOTAL         18045.10            6727.72         6269.49         5047.90

目前我有这个,差不多......

Transaction Type    AMOUNT (£) CREDIT   Product Type
Bank Transfer       653.89              Product type 1
Card                1323.24             Product type 1
Cash                4538.12             Product type 1
Direct Debit        118.80              Product type 1
Bank Transfer       653.89              Product type 2
Card                1323.24             Product type 2
Cash                4538.12             Product type 2
Direct Debit        118.80              Product type 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 

1 个答案:

答案 0 :(得分: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)