mercredi 24 juillet 2019

Where Am I Going Wrong with Count Distinct If Query?

I am having an issue with my query. I need to count the number of activated, non-cancelled parents who have a child whose b'day is in 2 weeks for each day from the 2nd of July onwards.

select d.date, 
count(distinct if(u.cancellation is null or date(u.cancellation) > d.date 
and concat(2019,right(u2.dob,6)) + interval 2 week = d.date,u.userId,null)) 

from

(select distinct date(created) 'date'
from dates
where created between '2019-07-02' and curdate()) d

join user u on date(u.activation) <= d.date
join account ac on ac.userId = u.userId and ac.accountType = 'PARENT'
join relations ur on ur.relatedId = u.userId and ur.type = 'CHILD'
join user u2 on u2.userId = ur.userId
group by d.date; 

I think I have gone wrong somewhere in the 'count(distinct if' part of the query as it does not seem to be taking into account this condition: concat(2019,right(u2.dob,6)) + interval 2 week = d.date

Is it not possible to put multiple and/or conditions in a count(distinct)?

Any insight would be appreciated.

Aucun commentaire:

Enregistrer un commentaire