Dev works always busy
This is a good event
Dev works always busy
This is a good event
With a ligh of accent-colorish ribbon on the left.
quote some text in here
this is still in it
now we exit block quotes, into the next
more robust
at line 2
and three
now we exit block quotes
一些代码,使用sqlglot库
import sqlglot
ast = sqlglot.parse_one(sql)
#include <iostream>
int main() {
return 0;
}
yeah yeah why not more
-- Cohort retention & revenue by first-purchase month, with a recursive calendar
-- Requires (example schemas):
-- customers(customer_id, created_at)
-- orders(order_id, customer_id, order_date, amount, status)
-- Notes:
-- - Cancels/refunds excluded via status
-- - Dedupes accidental dup orders
-- - Fills missing dates with a recursive calendar
-- - Produces cohort-month (M0, M1, …) retention and revenue
WITH
-- 1) Parameters you can tweak in one place
params AS (
SELECT
DATE '2024-01-01'::date AS start_date,
DATE '2025-10-31'::date AS end_date,
ARRAY['cancelled','refunded']::text[] AS bad_statuses
),
-- 2) Calendar: one row per date (WITH RECURSIVE)
calendar AS (
WITH RECURSIVE d AS (
SELECT p.start_date AS dt
FROM params p
UNION ALL
SELECT dt + INTERVAL '1 day'
FROM d, params p
WHERE dt + INTERVAL '1 day' <= p.end_date
)
SELECT dt::date AS dt
FROM d
),
-- 3) Clean orders: drop bad statuses and dedupe identical (customer, datetime, amount)
-- keeping the earliest order_id per tie.
orders_clean AS (
SELECT *
FROM (
SELECT
o.*,
ROW_NUMBER() OVER (
PARTITION BY o.customer_id, o.order_date, o.amount
ORDER BY o.order_id
) AS rn
FROM orders o
JOIN params p ON TRUE
WHERE o.status IS DISTINCT FROM ANY (p.bad_statuses)
AND o.order_date::date BETWEEN p.start_date AND p.end_date
) t
WHERE rn = 1
),
-- 4) First order per customer (cohort anchor)
first_order AS (
SELECT DISTINCT ON (oc.customer_id)
oc.customer_id,
oc.order_date::date AS first_order_date,
date_trunc('month', oc.order_date)::date AS cohort_month
FROM orders_clean oc
ORDER BY oc.customer_id, oc.order_date
),
-- 5) Daily active purchasers: did the customer purchase on that day?
daily_purchasers AS (
SELECT
c.dt,
oc.customer_id,
1 AS purchased_flag,
oc.amount
FROM calendar c
JOIN orders_clean oc
ON oc.order_date::date = c.dt
),
-- 6) Daily cohort-aligned metrics: join each purchase back to its cohort anchor
daily_cohort_metrics AS (
SELECT
f.cohort_month,
dp.dt AS activity_date,
-- month offset from first purchase month (M0 = cohort month)
(date_trunc('month', dp.dt) - f.cohort_month) / INTERVAL '1 month' AS month_offset,
dp.customer_id,
dp.purchased_flag,
dp.amount
FROM daily_purchasers dp
JOIN first_order f
ON f.customer_id = dp.customer_id
),
-- 7) Cohort sizes (denominator)
cohort_sizes AS (
SELECT
cohort_month,
COUNT(DISTINCT customer_id) AS cohort_customers
FROM first_order
GROUP BY cohort_month
),
-- 8) Monthly rollups per cohort & offset: active users and revenue
cohort_monthly AS (
SELECT
dcm.cohort_month,
dcm.month_offset::int AS month_offset,
COUNT(DISTINCT dcm.customer_id) AS active_customers,
SUM(dcm.amount) AS revenue
FROM daily_cohort_metrics dcm
GROUP BY dcm.cohort_month, dcm.month_offset
),
-- 9) Stitch sizes back in and compute retention, ARPU, LTV
cohort_final AS (
SELECT
cm.cohort_month,
cm.month_offset,
cs.cohort_customers,
cm.active_customers,
(cm.active_customers::numeric / NULLIF(cs.cohort_customers,0))::numeric(6,4) AS retention_rate,
cm.revenue,
(cm.revenue / NULLIF(cs.cohort_customers,0))::numeric(12,2) AS arpu, -- revenue per original user
SUM(cm.revenue) OVER (
PARTITION BY cm.cohort_month
ORDER BY cm.month_offset
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) / NULLIF(cs.cohort_customers,0) AS ltv_cumulative
FROM cohort_monthly cm
JOIN cohort_sizes cs USING (cohort_month)
)
-- 10) Final report: one row per cohort-month-offset
SELECT
cohort_month::date AS cohort_month,
CONCAT('M', LPAD(month_offset::text, 2, '0')) AS month_bucket,
cohort_customers,
active_customers,
retention_rate, -- e.g., 0.4230 = 42.30%
revenue,
arpu, -- revenue / original cohort size
ltv_cumulative -- running LTV since M0
FROM cohort_final
WHERE month_offset BETWEEN 0 AND 12
ORDER BY coh
this is unnamed block with default syntax highligh
无序项目,实心圆
无序项目(-也是)
无序项目(+可混用)
勾选可以直接在非编辑器中直接toggle
Mentions 用户 @calvin,组群 @trust_level_0
The quote whole topic just quotes the first post in a topic in a bbcode style quoting, but since the first is an event, it cannot appear syntatically in the following posts in this topic.