I'm hoping someone can check my syntax here. I have a users table subscribers
and an events table text_alert
.
I would like the total number of text alerts sent to each user split by before and after 2017-07-07
.
Here's my query:
select
subs.id as subscriber_id,
count(t.id) as total_alerts_received,
count(tbefore.id) as before_alerts_received,
count(tafter.id) as after_alerts_received
from subscriber subs
left join text_alert t on t.subscriber_id = subs.id
left join text_alert tbefore on tbefore.subscriber_id = subs.id and tbefore.create_date_time::date <= '2017-07-07'
left join text_alert tafter on tafter.subscriber_id = subs.id and tafter.create_date_time::date > '2017-07-07'
where subs.sms = 0
group by subs.id
My expectation was that the sum of before and after alerts_received
fields would match total_alerts_received
. What's happening instead is that the 3 fields have the same value. If subscriber 010
has ten alerts, then both before and after fields also have 10 alerts.
To split before and after I used left joins on the fact table alerts.
Any help much appreciated.