-- Email unique click rate -- Delivered emails sent in the month clicked at least once, 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 = 'click') 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