0

So, I have the following table (gross example):

CREATE TABLE budget_details(
    detail_id INT PRIMARY KEY,
    budget_id INT FOREIGN KEY,
    detail_month VARCHAR(16),
    total DECIMAL(18,2)
)

I'm trying to Select a Single Row with budget_id and have different columns for each Month (eg. January, February, etc) displaying their total, and all the other months null, showing 0, idc, something like this Example of results

I know it'd be easier to just recreate the table, but it's an old software and i'd break a lot of stuff

Any and all help is appreciated

1 Answer 1

2

The query below provides the result as shown in your image.

 SELECT 
   detail_id,
   budget_id, 
  CASE WHEN detail_month = 'January' THEN total END AS 'January',
  CASE WHEN detail_month = 'February' THEN total END AS 'February',
  CASE WHEN detail_month = 'March' THEN total END AS 'March'
FROM 
 budget_details
ORDER BY
   detail_id,
   budget_id
Sign up to request clarification or add additional context in comments.

3 Comments

Not quite the answer, because I'm just realizing I didn't post my question quite right, I get multiple rows per detail_id, each per month, i want a single row with all months, but i'll try to work it out from there, Thanks a lot!
Just aded SUM outside of the CASE method, and used GROUP BY, and got my full answer, again thanks a lot!
No worries - good to see you're on the right track

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.