Reputation: 27
I have a table that consists of questions and answers as shown:
I need to write a query that summarises it By question, listing the answers for each question, like below:
I seem to be getting hung up on the best way to write this query, any help would be much appreciated.
Upvotes: 1
Views: 24
Reputation: 40481
You can use conditional aggregation like this:
SELECT t.Form,t.Sequence,
MAX(CASE WHEN t.question_id = 101 then t.Answer end) as First_Name,
MAX(CASE WHEN t.question_id = 101 then t.Answer end) as Last_Name,
MAX(CASE WHEN t.question_id = 101 then t.Answer end) as Age
FROM YourTable t
GROUP BY t.Form,t.Sequence
Upvotes: 1