pivot table headache

Unfortunately, I'm still stuck with my pivot table problem.

My data table:
+---------------+------------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+---------------+------------------+------+-----+---------+
| respondent_id | int(10) unsigned | NO | MUL | 0 |
| stream_id | int(10) unsigned | NO | MUL | |
| survey_id | int(10) unsigned | NO | MUL | 0 |
| section_id | int(10) unsigned | NO | MUL | 0 |
| question_id | int(10) unsigned | NO | MUL | 0 |
| question_sub | enum('','C','I') | NO | | |
| response | text | NO | | |
+---------------+------------------+------+-----+---------+

With sample data...
+------+------+------+------+-----+----+----------+
| rID | stID | suID | seID | qID | QS | response |
+------+------+------+------+-----+----+----------+
| 6867 | 18 | 6 | 92 | 219 | | 1 |
| 6867 | 18 | 6 | 92 | 220 | | 2 |
| 6867 | 18 | 6 | 92 | 221 | | 3 |
| 6867 | 18 | 6 | 92 | 222 | | 5 |
| 6867 | 18 | 6 | 92 | 223 | | 7 |
| 6867 | 18 | 6 | 92 | 224 | | 9 |
| 6867 | 18 | 6 | 92 | 225 | | 7 |
| 6867 | 18 | 6 | 92 | 226 | | 5 |
| 6867 | 18 | 6 | 92 | 227 | | 3 |
| 6867 | 18 | 6 | 92 | 228 | | 3 |
| 6867 | 18 | 6 | 92 | 229 | | 3 |
| 6867 | 18 | 6 | 92 | 230 | | 3 |
| 6867 | 18 | 6 | 92 | 231 | | 3 |
| 6867 | 18 | 6 | 92 | 232 | | 3 |
| 6867 | 18 | 6 | 92 | 233 | | 3 |
| 6867 | 18 | 6 | 92 | 234 | | 3 |
| 6867 | 18 | 6 | 92 | 235 | | 3 |

... wonderfully normalised.

Now when I try this pivot table idea based on http://www.artfulsoftware.com/infotree/queries.php#523 I end up with what would be right, if not for the data not all being in a single row.

select respondent_id, IF(survey_id=6 AND section_id=92 AND question_id=219, response, '') AS 'Q_6_92_219', IF(survey_id=6 AND section_id=92 AND question_id=220, response, '') AS 'Q_6_92_220' FROM response WHERE stream_id=18 AND survey_id=6;

This query I can generate in code, so that's easy enough, but I end up with multiple lines for each respondent_id, instead of a single line with all responses per respondent. Not quite the idea...

+---------------+------------+------------+
| respondent_id | Q_6_92_219 | Q_6_92_220 |
+---------------+------------+------------+
| 6867 | 1 | |
| 6867 | | 2 |

.. so is there an easy(ish) way to rectify this last issue?

Comments