From a44eee78b7fc2e52c69835c563ba6165d3f09092 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Mon, 3 Aug 2026 10:14:51 -0700 Subject: [PATCH] Count only ACTIVE subscriptions in new-subscription analytics. Co-authored-by: Cursor --- db/query/analytics.sql | 12 +++++++++--- gen/db/analytics.sql.go | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/db/query/analytics.sql b/db/query/analytics.sql index 6294fe1d..d8cce075 100644 --- a/db/query/analytics.sql +++ b/db/query/analytics.sql @@ -151,13 +151,18 @@ ORDER BY d.date; SELECT COUNT(*)::bigint AS total, COUNT(*) FILTER (WHERE us.status = 'ACTIVE')::bigint AS active, - COUNT(*) FILTER (WHERE us.created_at::date = sqlc.arg('ref_date')::date)::bigint AS new_today, COUNT(*) FILTER ( - WHERE us.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '6 days' + WHERE us.status = 'ACTIVE' + AND us.created_at::date = sqlc.arg('ref_date')::date + )::bigint AS new_today, + COUNT(*) FILTER ( + WHERE us.status = 'ACTIVE' + AND us.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '6 days' AND us.created_at::date <= sqlc.arg('ref_date')::date )::bigint AS new_this_week, COUNT(*) FILTER ( - WHERE us.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '29 days' + WHERE us.status = 'ACTIVE' + AND us.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '29 days' AND us.created_at::date <= sqlc.arg('ref_date')::date )::bigint AS new_this_month FROM user_subscriptions us @@ -198,6 +203,7 @@ FROM generate_series( INTERVAL '1 day' ) AS d(date) LEFT JOIN user_subscriptions us ON us.created_at::date = d.date + AND us.status = 'ACTIVE' AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz) AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.created_at < sqlc.narg('range_end')::timestamptz) GROUP BY d.date diff --git a/gen/db/analytics.sql.go b/gen/db/analytics.sql.go index 9af81941..a9253c21 100644 --- a/gen/db/analytics.sql.go +++ b/gen/db/analytics.sql.go @@ -331,6 +331,7 @@ FROM generate_series( INTERVAL '1 day' ) AS d(date) LEFT JOIN user_subscriptions us ON us.created_at::date = d.date + AND us.status = 'ACTIVE' AND ($3::timestamptz IS NULL OR us.created_at >= $3::timestamptz) AND ($4::timestamptz IS NULL OR us.created_at < $4::timestamptz) GROUP BY d.date @@ -1410,13 +1411,18 @@ const AnalyticsSubscriptionsSummary = `-- name: AnalyticsSubscriptionsSummary :o SELECT COUNT(*)::bigint AS total, COUNT(*) FILTER (WHERE us.status = 'ACTIVE')::bigint AS active, - COUNT(*) FILTER (WHERE us.created_at::date = $1::date)::bigint AS new_today, COUNT(*) FILTER ( - WHERE us.created_at::date >= $1::date - INTERVAL '6 days' + WHERE us.status = 'ACTIVE' + AND us.created_at::date = $1::date + )::bigint AS new_today, + COUNT(*) FILTER ( + WHERE us.status = 'ACTIVE' + AND us.created_at::date >= $1::date - INTERVAL '6 days' AND us.created_at::date <= $1::date )::bigint AS new_this_week, COUNT(*) FILTER ( - WHERE us.created_at::date >= $1::date - INTERVAL '29 days' + WHERE us.status = 'ACTIVE' + AND us.created_at::date >= $1::date - INTERVAL '29 days' AND us.created_at::date <= $1::date )::bigint AS new_this_month FROM user_subscriptions us