select u.id
, u.created_at
, u.utm_source
, u.utm_medium
, u.utm_campaign
, ads.utm_source
, ads.utm_medium
, ads.utm_campaign
, ads.created_at
, case when
u.utm_source = ads.utm_source
and u.utm_medium = ads.utm_medium
and u.utm_campaign = ads.utm_campaign
then 'Yes'
else 'No'
end as lead_to_signup
, case when activities.id is null then 'No' else 'Yes' end as onboarded
from users as u
left join ad_clicks as ads
on ads.lead_id = u.id
and ads.created_at <= u.created_at
and datediff('days', ads.created_at, u.created_at) <= 90
left join (select id, candidate_id from activities where type = 'CandidateCompletedOnboarding') as activities on activities.candidate_id = u.id
where type = 'Candidate'
and u.utm_source <> 'organic'
and u.created_at >= '2018-08-01'
and u.utm_medium is not null
and u.utm_medium not in ('blog', 'social')