0

I have the following query result

+-----------+-------------+----------+----------+----------+
| person_id | question_id | answer_1 | answer_2 | answer_3 |
+-----------+-------------+----------+----------+----------+
|         1 |           1 | a1       |          |          |
|         1 |           2 |          | a2       |          |
|         1 |           3 |          |          | a3       |
|         2 |           1 | a4       |          |          |
|         2 |           2 |          | a5       |          |
|         2 |           3 |          |          | a6       |
+-----------+-------------+----------+----------+----------+

This was achieved using LEFT JOIN

How can i format this to result in the following

+-----------+----------+----------+----------+
| person_id | answer_1 | answer_2 | answer_3 |
+-----------+----------+----------+----------+
|         1 | a1       | a2       | a3       |
|         2 | a4       | a5       | a6       |
+-----------+----------+----------+----------+

The first query is a very big one, its using generated column names, so the answer its joined from different tables. Will be ideally to take the result from the first table and convert it to the second table.

2
SELECT person_id, MAX(answer_1), MAX(answer_2), MAX(answer_3)
FROM (<your_sub_query>) subquery_alias
GROUP BY person_id

would do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.