pepperwind / answer_key /kpis /email_unsubscribe_rate.sql
rasinmuhammed's picture
Pepperwind Trading Co. preview, version 1.0
27ec678 verified
Raw History Blame Contribute Delete
652 Bytes
-- Email unsubscribe rate
-- Delivered emails sent in the month that led to an unsubscribe, over delivered emails.
SELECT strftime(s.sent_at, '%Y-%m') AS month, count(*) FILTER (WHERE h.send_id IS NOT NULL) AS hit, count(*) AS delivered, count(*) FILTER (WHERE h.send_id IS NOT NULL)::DOUBLE / count(*) AS value FROM marketing.email_send s JOIN (SELECT DISTINCT send_id FROM marketing.email_event WHERE type = 'delivered') d USING (send_id) LEFT JOIN (SELECT DISTINCT send_id FROM marketing.email_event WHERE type = 'unsubscribe') h USING (send_id) WHERE s.sent_at >= TIMESTAMP '2023-10-01' AND s.sent_at < TIMESTAMP '2026-10-01' GROUP BY 1 ORDER BY 1