Question

I have this query

SELECT IF( sort_order =1, title, NULL ) AS q1, IF( sort_order =2, title, NULL ) AS q2,
       IF( sort_order =3, title, NULL ) AS q3
FROM  `choice` 
WHERE  `question_id` =1101

that displays this result

    q1    q2       q3
Pollster  NULL     NULL

NULL      Snooper  NULL

NULL      NULL     The Tank

Is there a way to group or order that will give me this result

    q1    q2       q3
Pollster  Snooper  The Tank
Was it helpful?

Solution

Yes. Just include aggregation functions:

SELECT max(IF( sort_order =1, title, NULL )) AS q1,
       max(IF( sort_order =2, title, NULL )) AS q2,
       max(IF( sort_order =3, title, NULL )) AS q3
FROM  `choice` 
WHERE  `question_id` = 1101;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top