我正在研究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
在任何时候,表格中的某个类别可能只有两行。有人可以帮我吗?
答案 0 :(得分: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