Clarkester
Clarkester

Reputation: 27

Summarising data from a Q/A table

I have a table that consists of questions and answers as shown: Table 1

I need to write a query that summarises it By question, listing the answers for each question, like below:

Required Summary Table

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

Answers (1)

sagi
sagi

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

Related Questions