Im trying to do a query with the following. I have simplified the query as much as possible for purposes of the question. It is basically for an email schedular app I am writing.
Two tables. An email can belong to a particular schedular. A schedular comprises of several emails. Each email that has been sent is marked with a specific step number. step1, step2, step3, step4, step5
email_schedular (id, datecreated, recipient)
email(id, email_schedular_id, text, subject, sent, stepNumber)
Now what im trying to do is determine in a report if a step has been sent or not. I have had a go at it but im not sure if im going about it the right way?
SELECT
ed.id,
ed.datecreated
ed.recipient
if(e.stepNumber= 1, 'Y', 'N') AS step_1_sent,
if(e.stepNumber= 2, 'Y', 'N') AS step_2_sent,
if(e.stepNumber= 3, 'Y', 'N') AS step_3_sent,
if(e.stepNumber= 4, 'Y', 'N') AS step_4_sent
FROM
email_echedular ed
JOIN email e ON (e.email_echedular_id = ed.id)
GROUP BY ed.id
Instead of using an
Nfor not sent, use aNULL. Then when you group them, you can take theMAX()value for each and eliminate the NULLs down to one row. The end result should be one row pered.idwith aYesfor sent steps andNULLfor unsent steps.If you really don’t want a
NULLfor the non-sent columns, you can wrap those inCOALESCE()to replace theNULLwithN:Edit Changed to
LEFT JOINand fixed join condition…