Pathubs SQL Curriculum • Module 19

SQL Time-Based Analysis & Trend Analytics

Master temporal analytics in SQL: group transactions by day, week, month, and year, calculate Period-over-Period (MoM & YoY) growth with LAG(), track cumulative Year-to-Date running revenue, analyze weekday seasonality, and interpret business trends.

⏱️ Estimated Time:35 Minutes
🎯 Level:Time-Series Analytics • Business Intelligence
📊 Track:Growth Metrics & Seasonality Modeling
✨ Mode:Live Time-Series SQL Engine
1

Real Business Question: Temporal Velocity

In business analytics, a static number like "We made $500,000 in revenue" is meaningless without time context. Did that $500,000 arrive over 3 days, 6 months, or 2 years? Is revenue accelerating, plateauing, or decaying?

Core Analytical Principle
Raw database transactions record exact timestamps (e.g. 2026-01-15 14:23:45). Before analysis, analysts normalize granular timestamps into standard reporting buckets: Days, Weeks, Months, Quarters, or Years.
2

Time-Based Grouping: Macro vs. Micro Windows

Time Granularity Hierarchy

Timestamp
Exact transaction second
──▶

Daily
Operational tracking
──▶

Monthly (MoM)
Core business reporting
──▶

Yearly (YoY)
Long-term strategic trend
4

Monthly Sales & Date Extraction

SELECT
    SUBSTRING(order_date, 1, 7) AS month,
    COUNT(*) AS orders,
    SUM(sales) AS monthly_revenue
FROM orders
GROUP BY month
ORDER BY month ASC;
5

Month-over-Month (MoM) Growth with LAG()

WITH monthly_sales AS (
    SELECT
        SUBSTRING(order_date, 1, 7) AS month,
        SUM(sales) AS revenue
    FROM orders
    GROUP BY month
)
SELECT
    month,
    revenue,
    LAG(revenue) OVER (ORDER BY month) AS prev_month_revenue,
    revenue - LAG(revenue) OVER (ORDER BY month) AS dollar_change,
    ROUND(((revenue - LAG(revenue) OVER (ORDER BY month)) / NULLIF(LAG(revenue) OVER (ORDER BY month), 0)) * 100, 2) || '%' AS percentage_growth
FROM monthly_sales
ORDER BY month ASC;
6

Year-over-Year (YoY) Seasonality Controls

Why do analysts prioritize YoY comparisons? In retail, comparing January against December usually shows a steep decline because December has Christmas & holiday surges. Comparing January 2026 vs. January 2025 eliminates seasonal noise and reveals true year-over-year organic growth.

9

Missing Periods & Continuous Timelines

Crucial Gotcha
If there were no sales transactions in February, a standard GROUP BY month query will simply omit February entirely! The rows will jump from January directly to March. In production reporting, analysts join against a calendar date dimension table to keep continuous zeroes.
10

Interpreting Trends Like an Analytics Pro

1. Upward Trend (Growth)

Consistently higher monthly peaks and troughs over consecutive quarters.

2. Downward Trend (Churn)

Decaying transaction volume, lower AOV, or declining YoY cohorts.

3. Seasonality (Cycles)

Predictable spikes recurring on specific weekdays or annual quarters.

⚡ Live Interactive SQL Time-Based Analysis Lab
Execute live time-series SQL: daily breakdowns, monthly trends, MoM variance with LAG(), and weekday seasonality
Challenge 1 — Monthly Sales AggregationTemporal Case Study

Calculate total orders and total sales revenue for each month. Format month as "YYYY-MM" and sort chronologically.

SQL Query Editor:
Query Execution Result:
TEST YOUR KNOWLEDGE

Knowledge Assessment: SQL Time-Based Analysis

Test your applied temporal reasoning on monthly groupings, MoM percentage growth, YoY seasonality, missing periods, and running totals.

Question 1 of 10Current Score: 0 / 0
Q1

Why is grouping by month (e.g. SUBSTRING(order_date, 1, 7)) different from grouping by exact date?