我需要为此报表获取此视图。在sql中翻转数据
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
我相信你实际上是在寻找枢:http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a -simple-sample.aspx – 2013-02-18 16:25:00