2017-09-11 93 views
0

我已經編寫了SQL查詢以從表中獲取數據。我的查詢是:這個SQL查詢的如何在SQL Server中將數據從行顯示到列中

SELECT 
    terminal_group_name, STN, payment_mode_name, RA_Amount 
INTO 
    #t 
FROM 
    view_financial_report_2 
WHERE 
    TrDate >= CONVERT(VARCHAR, CAST('2017-09-11' AS DATE), 111) 
    AND TrDate <= CONVERT(VARCHAR, CAST('2017-09-11' AS DATE), 111); 

SELECT 
    SUM(t.RA_Amount) as VAL, t.payment_mode_name, t.STN 
FROM 
    #t t 
GROUP BY 
    t.payment_mode_name, t.STN 

DROP TABLE #t; 

輸出是:

VAL | payment_mode_name | STN 
5000| CASH    | City1 
2000| FOC    | City1 
1500| CASH    | City2 

但是我想在以下格式的數據:

STN | CASH AMOUNT | FOC AMOUNT 
City1| 5000  | 2000 
City2| 1500  | 0 

誰能幫我這個查詢?

PS:我試圖用這個支點的查詢,但是該查詢拋出僵局例外:

SELECT 
    terminal_group_name 
,STN 
,ISNULL([Cash], 0) AS Cash 
,ISNULL([To Pay], 0) AS ToPay 
,ISNULL([TO PAY COLLECTION], 0) AS ToPaycollection 
,ISNULL([Credit], 0) AS Credit 
,ISNULL([Foc], 0) AS Foc 
,ISNULL([Advance], 0) AS Advance 
,ISNULL([CUSTOMER RECEIPT], 0) AS CustomerReceipt 
FROM (SELECT 
    terminal_group_name 
    ,STN 
    ,payment_mode_name 
    ,RA_Amount 
    FROM view_financial_report_2 
    WHERE TrDate >= CONVERT(VARCHAR, CAST('2017-09-11' AS DATE), 111) 
    AND TrDate <= CONVERT(VARCHAR, CAST('2017-09-11' AS DATE), 111)) ps PIVOT (SUM(RA_Amount) FOR payment_mode_name IN ([Cash], [To Pay], [Credit], [Foc], [CUSTOMER RECEIPT], [TO PAY COLLECTION], [Advance])) AS pvt; 

view_financial_report_2:

SELECT  dbo.tbl_terminal_group.terminal_group_name, dbo.view_financial_report_1.STID, dbo.view_financial_report_1.STN, dbo.view_financial_report_1.TrDate, 
         dbo.view_financial_report_1.payment_mode_name, dbo.view_financial_report_1.RA_Amount, dbo.view_financial_report_1.RD_Amount 
FROM   dbo.tbl_terminal_group INNER JOIN 
         dbo.tbl_terminal_master ON dbo.tbl_terminal_group.terminal_group_id = dbo.tbl_terminal_master.terminal_group_id INNER JOIN 
         dbo.view_financial_report_1 ON dbo.tbl_terminal_master.terminal_id = dbo.view_financial_report_1.STID; 

view_financial_report_1:

SELECT  STID, STN, CONVERT(VARCHAR(10), booking_datetime, 111) AS TrDate, payment_mode_name, SUM(RA_Amount) AS RA_Amount, SUM(RD_Amount) 
         AS RD_Amount 
FROM   dbo.view_Booking_Summary 
GROUP BY STID, STN, booking_datetime, payment_mode_name 
UNION ALL 
SELECT  source_terminal_id, source_terminal_name, CONVERT(VARCHAR(10), edit_date, 111) AS TrDate, payment_mode_name, SUM(RA_Amount * - 1) AS RA_Amount, 
         SUM(RD_Amount * - 1) AS RD_Amount 
FROM   view_Booking_Cancel 
GROUP BY source_terminal_id, source_terminal_name, edit_date, payment_mode_name 
UNION ALL 
SELECT  DTID, DTN, CONVERT(VARCHAR(10), delivery_datetime, 111) AS TrDate, 'CASH' AS payment_mode_name, SUM(RASR_Amount) AS RA_Amount, SUM(RDSR_Amount) 
         AS RD_Amount 
FROM   view_delivery_master_report 
WHERE  RASR_Amount > 0 
GROUP BY DTID, DTN, delivery_datetime, payment_mode_name 
UNION ALL 
SELECT  DTID, DTN, CONVERT(VARCHAR(10), delivery_datetime, 111) AS TrDate, 'CASH' AS payment_mode_name, SUM(RAST_Amount) AS RA_Amount, SUM(RDST_Amount) 
         AS RD_Amount 
FROM   view_delivery_master_report 
WHERE  RAST_Amount > 0 
GROUP BY DTID, DTN, delivery_datetime, payment_mode_name 
UNION ALL 
SELECT  DTID, DTN, CONVERT(VARCHAR(10), delivery_datetime, 111) AS TrDate, 'TO PAY COLLECTION' AS payment_mode_name, SUM(RD_Amount) AS RA_Amount, 
         SUM(RD_Amount) AS RD_Amount 
FROM   view_delivery_master_report 
WHERE  RD_Amount > 0 
GROUP BY DTID, DTN, delivery_datetime 
UNION ALL 
SELECT  DTID, DTN, CONVERT(VARCHAR(10), tdate, 111) AS TrDate, 'CASH' AS payment_mode_name, SUM(amount) AS RA_Amount, SUM(amount) AS RD_Amount 
FROM   view_old_software_sale 
GROUP BY DTID, DTN, tdate 
UNION ALL 
SELECT terminal_id, terminal_name, CONVERT(VARCHAR(10), payment_date, 111) AS TrDate, 'CUSTOMER RECEIPT' as payment_mode_name, SUM(total) as RA_Amount, SUM(total) as RD_Amount 
FROM view_customer_payment_master 
GROUP BY terminal_id, terminal_name, CONVERT(VARCHAR(10), payment_date, 111) 
+0

如果您使用的是SQL SERVER 2008,請查看[PIVOT](https://docs.microsoft.com/zh-cn/sql/t-sql/queries/from-using-pivot-and-c) '或以上。 –

+0

@HamletHakobyan是否有任何其他的樞軸方式? –

+0

樞軸的問題是什麼? –

回答

0

嘗試此查詢 -

SELECT t.STN 
     ,SUM(CASE WHEN t.payment_mode_name = 'CASH' THEN t.RA_Amount) 
     ,SUM(CASE WHEN t.payment_mode_name = 'FOC' THEN t.RA_Amount) 
FROM 
#t t 
GROUP BY 
t.STN 
相關問題