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.
-
1Please consider reading this advicemustaccio– mustaccio2019年05月10日 12:51:11 +00:00Commented May 10, 2019 at 12:51
1 Answer 1
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
lang-sql