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.

asked May 10, 2019 at 7:29
1
  • 1
    Please consider reading this advice Commented May 10, 2019 at 12:51

1 Answer 1

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.

answered May 10, 2019 at 10:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.