I have the following structure from a huge query with subqueries for every column. The goal is to have the non-NULL value for every column first.
+-----+--------+--------+--------+-------+
| id | val1 | val2 | val3 | val4 |
+-----+--------+--------+--------+-------+
| 1 | STR1 | NULL | NULL | NULL |
| 1 | NULL | STR2 | NULL | NULL |
| 1 | NULL | NULL | STR3 | NULL |
| 1 | NULL | NULL | NULL | STR4 |
| 2 | STR1 | NULL | NULL | NULL |
| 2 | NULL | STR2 | NULL | NULL |
+-----+--------+--------+--------+-------+
Desired result:
+-----+--------+--------+--------+-------+
| id | val1 | val2 | val3 | val4 |
+-----+--------+--------+--------+-------+
| 1 | STR1 | STR2 | STR3 | STR4 |
| 2 | STR1 | STR2 | NULL | NULL |
+-----+--------+--------+--------+-------+
My goal is to group by ID but have all non NULL-fields from the query first. I tried to use it with the MAX
function as well as with GROUP_CONCAT
but both were not what i searched for.
Do you have an idea on how to achieve this? Any help is highly appreicated.
Best endo
edit: put in the correct ordering together with max solved it. I'm sorry and a bit ashamed I bothered you with this.