0

我正在研究 ans SQL server 数据库,以将结果转换为特定格式。我在表中的数据为

Category      AmountType       EligibleAmount  NonEligibleAmount

 100              FA               1500.00       1200.00
 100              IA               2000.00       1300.00
 200              FA               1000.00        800.00
 200              IA               3500.00       2100.00

我希望输出为

Category  FA EligibleAmt  FA NonEligibleAmt  IA EligibleAmt   IA NonEligibleAmt  

 100        1500.00          1200.00              2000.00        1300.00
 200        1000.00           800.00              3500.00        2100.00

在任何时候,表格中的一个类别可能只有两行。有人可以帮我吗?

4

1 回答 1

1

我认为这会有所帮助:

SELECT CATEGORY, MAX(DISTINCT CASE WHEN AMOUNTTYPE = 'FA' THEN ELIGIBLEAMOUNT END) AS [FA ELIGIBLE], 
                 MAX(DISTINCT CASE WHEN AMOUNTTYPE = 'FA' THEN NONELIGIBLEAMOUNT END) AS [FA NON ELIGIBLE], 
                 MAX(DISTINCT CASE WHEN AMOUNTTYPE = 'IA' THEN ELIGIBLEAMOUNT END) AS [IA ELIGIBLE], 
                 MAX(DISTINCT CASE WHEN AMOUNTTYPE = 'IA' THEN NONELIGIBLEAMOUNT END) AS [IA NON ELIGIBLE]
FROM  dbo.TESTTABLE
GROUP BY CATEGORY
于 2013-08-08T05:46:31.957 回答