Marketing & conversion analytics · multi-table SQL report generator

The Conversion Ledger

Four aggregate queries joined across marketing_campaigns · website_visitors · conversions — computed locally over a deterministic, seeded dataset of 9,000 sessions.

—
Report generated —
QUERY 0101

Marketing ROI Analysis

Campaign spend vs. attributed revenue — sessions credited to a campaign flight, conversions joined on visitor. Budgets are not prorated for clipped flights.

ROI by campaign — bars grow from the break-even axis; vermilion marks spend exceeding attributed revenue. Hover a row for attribution detail.
View SQL — Query 01 · marketing_campaigns → website_visitors → conversions
/* Query 01 — Marketing ROI Analysis

   Attribution model: a session is credited to a campaign when its date falls
   inside the campaign flight. Flights never overlap in this dataset, so each
   session maps to at most one campaign. Conversions join on visitor_id, so —
   as written — a visitor contributes their ENTIRE conversion history, which
   is realistic (campaigns keep converting after the flight ends). */
SELECT mc.name,
       COUNT(DISTINCT wv.visitor_id)   AS visitors,
       COUNT(DISTINCT c.conversion_id) AS conversions,
       SUM(c.value)                    AS total_revenue,
       mc.budget_spent,
       (SUM(c.value) - mc.budget_spent) / mc.budget_spent * 100 AS roi_percent
FROM marketing_campaigns mc
LEFT JOIN website_visitors wv
       ON DATE(wv.session_start) BETWEEN mc.start_date AND mc.end_date
LEFT JOIN conversions c
       ON wv.visitor_id = c.visitor_id
GROUP BY mc.campaign_id, mc.name   -- name added for display; grouped by id
ORDER BY roi_percent DESC;
QUERY 0202

Funnel Conversion Rates

Stage-by-stage drop-off for the selected window. Stages nest: a visitor must view 3+ pages before a signup is possible, and every buyer has signed up.

Stage heights are linear in visitor counts. Annotations between stages show step-through rate and absolute drop-off; the largest leak is drawn in vermilion.
View SQL — Query 02 · website_visitors → conversions
/* Query 02 — Funnel Conversion Rates (trailing window)

   Note on semantics: SUM(CASE …) counts conversion ROWS, not visitors. A
   repeat buyer contributes two 'purchase' rows, so `purchases` is an
   order-count. The funnel chart heights its stages on COUNT(DISTINCT
   buyers) while this table reports orders, exactly as specified. */
SELECT wv.source,
       COUNT(DISTINCT wv.visitor_id) AS total_visitors,
       SUM(CASE WHEN c.conversion_type = 'signup'   THEN 1 ELSE 0 END) AS signups,
       SUM(CASE WHEN c.conversion_type = 'purchase' THEN 1 ELSE 0 END) AS purchases,
       (SUM(CASE WHEN c.conversion_type = 'purchase' THEN 1 ELSE 0 END) * 1.0
          / COUNT(DISTINCT wv.visitor_id)) * 100 AS conversion_rate
FROM website_visitors wv
LEFT JOIN conversions c ON wv.visitor_id = c.visitor_id
WHERE wv.session_start >= DATE_SUB(NOW(), INTERVAL 30 DAY)  -- window parameterised in-app
GROUP BY wv.source
ORDER BY total_visitors DESC;
QUERY 0303

Cohort Analysis

Monthly session cohorts — volume, engagement depth and conversion rate, rendered as a column-normalised heat map.

Cell shading is column-normalised — each metric column is scaled to its own maximum, so darker means stronger within that column, not across columns.
View SQL — Query 03 · website_visitors → conversions
/* Query 03 — Cohort Analysis (monthly)

   Corrected numerator: the brief's COUNT(DISTINCT conversion_id) counts
   events, so a visitor holding both a signup AND a purchase would push the
   rate above 100%. Counting converting VISITORS keeps the ratio in [0,100]. */
SELECT DATE_FORMAT(wv.session_start, '%Y-%m') AS cohort_month,
       COUNT(DISTINCT wv.visitor_id)          AS visitors,
       AVG(wv.pages_viewed)                   AS avg_pages_per_session,
       COUNT(DISTINCT CASE WHEN c.conversion_id IS NOT NULL
                           THEN wv.visitor_id END)
         / COUNT(DISTINCT wv.visitor_id) * 100 AS conversion_rate_percent
FROM website_visitors wv
LEFT JOIN conversions c ON wv.visitor_id = c.visitor_id
GROUP BY DATE_FORMAT(wv.session_start, '%Y-%m')
ORDER BY cohort_month;
QUERY 0404

Customer Lifetime Value by Source

Revenue per purchaser by acquisition source. Lifetime value vs. average order value diverge wherever repeat buyers exist (~18% of this dataset).

The trailing bar encodes lifetime value relative to the best source.
View SQL — Query 04 · website_visitors ⟜ conversions (purchase only)
/* Query 04 — Customer Lifetime Value by Source

   Corrected denominator: the brief divided revenue by COUNT(DISTINCT
   conversion_id), which reproduces avg_order_value whenever a customer may
   place more than one order. Revenue / unique CUSTOMERS is the true lifetime
   value; ~18% of buyers in this dataset order twice, so the two diverge. */
SELECT wv.source,
       COUNT(DISTINCT wv.visitor_id)          AS unique_customers,
       AVG(c.value)                           AS avg_order_value,
       SUM(c.value) / COUNT(DISTINCT wv.visitor_id) AS lifetime_value,
       SUM(c.value)                           AS total_revenue
FROM website_visitors wv
JOIN conversions c
  ON wv.visitor_id = c.visitor_id
 AND c.conversion_type = 'purchase'
GROUP BY wv.source
ORDER BY lifetime_value DESC;