0

I am working on ans SQL server database, to bring the result in a specific format. I have the data in the table as

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

I want the output to be as

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

There could be only two rows for a category in the table at any point of time.Could anyone help me in this please?

2
  • If you don't care which row you get for each category, you could just use GROUP BY in your select. Check out the documentation for GROUP BY Commented Aug 7, 2013 at 3:43
  • I don't want to group the rows, then I have to sum the amount values which I don't want to do. I want to bring the second row amount values in column format. Commented Aug 7, 2013 at 3:55

1 Answer 1

1

I think this will help:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.