QUERYDESK
analytics_prod · MySQL 8.0 · 4 tables · 0 rows
→
Report suite · marketing & conversion analytics

Four joins.
One revenue truth.

A working query engine over marketing_campaigns, website_visitors, conversions and traffic_sources — date-range aware, fan-out checked, and every aggregate recomputed in the browser the moment you move a filter.

4source tables
7documented joins
23aggregate calls
0 mslast full run
mysql> analytics_prod — query_01_marketing_roi.sql
PARSEANALYZEJOIN ORDER SCANAGGREGATESORT
idle
Revenue in range$0
vs prior period
Attributed spend$0
vs prior period
Blended ROI0%
ROAS 0×
Distinct visitors0
vs prior period
Purchases0
AOV $0
Visit→purchase0%
vs prior period
01

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.

2 LEFT JOINCOUNT DISTINCT ×2SUMderived ratioGROUP BY campaign_id
+ refinement line added by the report engine▨ highlighted literal injected by the date-range control
Formula roi_percent = (SUM(c.value) − budget_spent) / budget_spent × 100
02

Funnel 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.

LEFT JOINSUM(CASE) ×3COUNT DISTINCTGROUP BY source4-stage funnel
+ refinement line added by the report engine▨ highlighted literal injected by the date-range control
Stage-to-stage drop_off = 1 − (stage_n / stage_n−1)Overall conversion_rate = purchases / visitors × 100
03

Cohort 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.

CTE + SELF JOINMIN() OVER cohortAVGTIMESTAMPDIFF2 GROUP BY levels
+ refinement line added by the report engine▨ highlighted literal injected by the date-range control
Offset TIMESTAMPDIFF(MONTH, first_seen, session_month) — calendar months, not 30-day blocksBlank cells = cohort too young for that offset
04

Customer 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.

INNER JOINWHERE type='purchase'AVG / SUM / COUNT DISTINCTfrequency ratiobubble scatter
+ refinement line added by the report engine▨ highlighted literal injected by the date-range control
⚠ 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)
engine.log — execution trace