Marketing ROI by campaign
Campaign spine with a date-window LEFT JOIN to sessions, then a second LEFT JOIN to conversions. Every join is left-anchored so unfunded campaigns still surface with zero revenue.
roi_percent = (SUM(c.value) â budget_spent) / budget_spent à 100Funnel conversion rates by source
Conditional aggregation (CASE) over a LEFT JOIN so visitors who never convert are still
counted in the denominator. Stage 2 âengagedâ = any session with ⥠3 pages viewed.
drop_off = 1 â (stage_n / stage_nâ1)Overall conversion_rate = purchases / visitors à 100Cohort analysis & retention heat map
Base aggregation groups sessions by activity month. The extended statement adds a CTE + self-join on
website_visitors to build a cohort-month à month-offset matrix.
TIMESTAMPDIFF(MONTH, first_seen, session_month) â calendar months, not 30-day blocksBlank cells = cohort too young for that offsetCustomer lifetime value by source
INNER JOIN (purchase-only) with per-source distinct customer counts. The specified
lifetime_value is retained verbatim and a corrected per-customer LTV is computed beside it.
SUM(c.value)/COUNT(c.conversion_id) is arithmetically identical to AVG(c.value) â it is order value, not lifetime value.Corrected: ltv_per_customer = SUM(value)/COUNT(DISTINCT visitor_id)