It seems as if only field created_date
ties together the records from which you want to take full_name
and email
. Given that, you could group by this created_date
and use an aggregate function (e.g. min
) considering only the answer
-values of those records with a particular field_type
-value:
select
min(CASE field_type WHEN 'full_name' THEN answer ELSE NULL END) as 'fullname',
min(CASE field_type WHEN 'email' THEN answer ELSE NULL END) as 'email'
FROM `ds_tbl_event_question` q
LEFT JOIN `ds_tbl_event_participant` p ON q.event_id = p.event_id AND p.question_id = q.event_question_id
LEFT JOIN `ds_events` e ON e.event_id = q.event_id
WHERE (
q.field_type = 'email'
OR q.field_type = 'full_name'
)
AND e.store_code = 'H9OCEN'
group by created_date
Note that the aggregate function (here: min
) is necessary because a select-clause must not contain other attributes than those listed in a group by clause except when used in an aggregate function.
In above query I tried to adapt your query as little as possible. Actually the query could be further shortened: The left joins actually turn into a join as you use a WHERE
-condition on left hand side values; and the condition on the field_type
is superfluous as the case
-statements do not consider any other values at all:
select
min(CASE field_type WHEN 'full_name' THEN answer ELSE NULL END) as 'fullname',
min(CASE field_type WHEN 'email' THEN answer ELSE NULL END) as 'email'
FROM `ds_tbl_event_question` q
JOIN `ds_tbl_event_participant` p ON q.event_id = p.event_id AND p.question_id = q.event_question_id
JOIN `ds_events` e ON e.event_id = q.event_id AND e.store_code = 'H9OCEN'
group by created_date