Skip to content

Instantly share code, notes, and snippets.

@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
@yancya
yancya / EXCEPT.sql
Created December 15, 2017 11:04
INTERSECT and EXCEPT for BigQuery
#standardsql
WITH a AS (
SELECT * FROM UNNEST([1,2,3,4]) AS n
), b AS (
SELECT * FROM UNNEST([4,5,6,7]) AS n)
SELECT * FROM a