Skip to content

Instantly share code, notes, and snippets.

View bassemawhoob's full-sized avatar

Bassem Mawhoob bassemawhoob

View GitHub Profile
@bassemawhoob
bassemawhoob / cohort.sql
Last active April 12, 2024 18:15
Revenue Retention Cohort Analysis - PostgreSQL
-- Based on: https://medium.com/quick-code/how-to-write-sql-to-calculate-user-cohort-retention-a1b3b57c7a2f
-- Uses DATE_PART instead of DATE_DIFF as it is not supported by PostgreSQL
-- (branch_id, scheduled_for, total_cents)
WITH activities AS (
SELECT branch_id, scheduled_for, total_cents
FROM orders
WHERE orders.status = 'complete'
),
-- (branch_id, cohort_month): cohort month is the first order date