Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created December 1, 2017 23:02
Show Gist options
  • Save ryantuck/2cc3167df00340dedf4e744d0bcdc5ee to your computer and use it in GitHub Desktop.
Save ryantuck/2cc3167df00340dedf4e744d0bcdc5ee to your computer and use it in GitHub Desktop.
Some SQL 201 tricks for parsing email data
with
email_data as (
select
s.id as send_id,
s.template,
s.sent_on,
o.opened_on,
case
when opened_on is null then false
else true
end as was_opened,
opened_on - sent_on as num_days
from
email.sends s
left join
email.opens o
on o.send_id = s.id
),
email_aggregate as (
select
template,
sum(
case
when was_opened is true then 1
else 0
end
) as num_opens,
count(*) as num_sends
from
email_data
group by template
)
select
*,
num_opens / num_sends::numeric as open_rate,
(round(num_opens / num_sends::numeric, 2) * 100)::text || ' %' as pretty_open_rate
from email_aggregate
limit 100
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment