This is the synthesis module. Everything you’ve built so far — retrieving “now” safely, extracting periods, calculating durations, and formatting for presentation — comes together here into the composite patterns that power real business dashboards: rolling windows, MTD/QTD/YTD reporting, fiscal calendars, tenure and SLA analytics, and the foundations of cohort analysis.
Business date analytics is not a new set of functions — it’s a set of patterns built from the functions you already know, applied to the recurring questions every company asks: How are we trending? Who’s overdue? How long has this been going on?
No stakeholder asks for DATEDIFF() output directly — they ask “how’s this quarter tracking against last quarter?” or “which customers are at risk of churn based on inactivity?” This module is where individual date functions become business answers.
A single well-built “trailing 30 days” query becomes the foundation of an entire team’s daily operating rhythm — refreshed automatically every morning, always correct, never requiring manual date updates. The patterns in this file are the ones repeated, with minor variation, across nearly every analytics team in every industry.
This module composes functions from every prior file in the module:
CURRENT_DATE, DATE_ADD/DATE_SUB, DATEDIFF, TIMESTAMPDIFF, YEAR/MONTH/QUARTER, DATE_FORMAT, alongside CASE, JOIN, and CTEs from earlier modules in the handbook.
Trailing N-day window:
WHERE order_date >= CURRENT_DATE - INTERVAL 30 DAY
AND order_date < CURRENT_DATE + INTERVAL 1 DAY
Month-to-date (MTD):
WHERE order_date >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
AND order_date < CURRENT_DATE + INTERVAL 1 DAY
Year-to-date (YTD):
WHERE order_date >= DATE_FORMAT(CURRENT_DATE, '%Y-01-01')
AND order_date < CURRENT_DATE + INTERVAL 1 DAY
Quarter-to-date (QTD) requires computing the current quarter’s start month explicitly (shown in the walkthrough and SQL file).
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
└────Q1────┘ └────Q2────┘ └────Q3────┘ └────Q4────┘
▲
CURRENT_DATE (e.g., July 7)
MTD : Jul 1 ─────────────────────► Jul 7
QTD : Jul 1 ─────────────────────► Jul 7 (Q3 started July 1)
YTD : Jan 1 ─────────────────────────────────────────────► Jul 7
Trailing 30 days: ◄──────────── 30 days ────────────► Jul 7
>= start AND < end) for every boundary — this handles time-of-day correctly on DATETIME columns and avoids off-by-one-day errors.TIMESTAMPDIFF() discipline from Module 03 — pick the unit that matches the business question exactly.QUARTER()) are wrong for any company whose fiscal year doesn’t start in January — always confirm the fiscal year start before reusing calendar-quarter logic.TIMESTAMPDIFF(YEAR, ...) over manual day-count division for age or tenure in years.signup_date instead of DATE_FORMAT(signup_date, '%Y-%m-01') produces one cohort per day instead of one per month, making a cohort retention grid practically unusable.CURRENT_DATE-derived boundaries multiple times in the same query with slightly different logic, producing internally inconsistent results.BETWEEN for date ranges instead of half-open comparisons, causing edge-of-range inclusion errors on DATETIME columns.“How would you write a query for month-to-date revenue that is correct on any day of the month, including the 1st?”
Filter with order_date >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-01') AND order_date < CURRENT_DATE + INTERVAL 1 DAY — this is correct even when CURRENT_DATE itself is the first of the month.
“A finance team’s fiscal year starts in April. How does this change your quarter calculation?”
Calendar-quarter logic (QUARTER()) cannot be used directly; the fiscal quarter must be computed by first shifting the month relative to the fiscal year start (e.g., April = fiscal month 1) before deriving the quarter.
“How would you build the foundation of a cohort retention report?”
Bucket users by the truncated signup period (e.g., signup month via DATE_FORMAT(signup_date, '%Y-%m-01')), then join or aggregate subsequent activity against that cohort bucket — never bucket by exact signup date.
Business date analytics is where the individual functions from Modules 01–04 combine into the patterns that power real dashboards: rolling windows, MTD/QTD/YTD reporting, fiscal calendars, tenure and SLA measurement, and cohort bucketing foundations. The unifying engineering discipline across every pattern in this file is: compute boundaries once, use half-open ranges, respect the business’s actual fiscal calendar, and never let a “quick” ad hoc query replace a consistent, reusable pattern.
CASE expression that correctly labels each order_date with its fiscal quarter (Q1–Q4).Previous: ← 04 — Date Formatting Back to: Module 09 README