如何在SQL Server中将行数据显示为列

时间:2017-09-11 08:38:32

标签: sql-server

我编写了SQL查询来从表中获取数据。我的疑问是:

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;

此SQL查询的输出为:

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)

1 个答案:

答案 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