Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Period-over-Period Comparisons (MoM, YoY) | Analytical SQL Techniques
SQL for Analytical Reports
Sezione 1. Capitolo 4
single

single

bookPeriod-over-Period Comparisons (MoM, YoY)

Scorri per mostrare il menu

Period-over-period analysis is a core technique in business analytics, allowing you to measure growth, trends, and changes by comparing metrics across consistent time intervals. Common period-over-period metrics include month-over-month (MoM) and year-over-year (YoY) comparisons. These analyses help you quickly identify whether performance is improving, declining, or stable.

SQL provides powerful tools for these comparisons, especially through the use of window functions like LAG and LEAD, which enable you to reference values from previous rows in a partitioned and ordered set. Date functions such as DATE_TRUNC, EXTRACT, and arithmetic operations on dates allow you to group, filter, and align data by specific periods, making it possible to compare like-for-like intervals. By combining window functions with date logic, you can efficiently calculate changes between periods directly in your queries.

Note
Definition
  • MoM (Month-over-Month): Measures the percentage or absolute change in a metric from one month to the next;
  • YoY (Year-over-Year): Compares the same metric for a specific period (such as a month or quarter) to the same period in the previous year, revealing seasonal trends and annual growth.
12345678
SELECT DATE_TRUNC('month', order_date) AS month, SUM(total) AS monthly_sales, LAG(SUM(total)) OVER (ORDER BY DATE_TRUNC('month', order_date)) AS last_month_sales, SUM(total) - LAG(SUM(total)) OVER (ORDER BY DATE_TRUNC('month', order_date)) AS sales_difference FROM orders GROUP BY DATE_TRUNC('month', order_date) ORDER BY month;
copy

The marketing team often requests a report that tracks monthly active users and highlights their MoM growth rate. This kind of analysis helps you spot engagement trends, evaluate the impact of campaigns, and plan future initiatives more effectively.

12345678910111213
SELECT DATE_TRUNC('month', signup_date) AS month, COUNT(DISTINCT user_id) AS active_users, LAG(COUNT(DISTINCT user_id)) OVER (ORDER BY DATE_TRUNC('month', signup_date)) AS last_month_active, ROUND( 100.0 * (COUNT(DISTINCT user_id) - LAG(COUNT(DISTINCT user_id)) OVER (ORDER BY DATE_TRUNC('month', signup_date))) / NULLIF(LAG(COUNT(DISTINCT user_id)) OVER (ORDER BY DATE_TRUNC('month', signup_date)), 0), 2 ) AS mom_growth_percent FROM users WHERE is_active = TRUE GROUP BY DATE_TRUNC('month', signup_date) ORDER BY month;
copy
Compito

Scorri per iniziare a programmare

Write a query to show the year-over-year (YoY) revenue change for each product category.

  • For each product category and year, calculate the total revenue.
  • Show the previous year's total revenue for the same category.
  • Compute the difference in revenue between the current year and the previous year for each category.
  • The query should include the columns: category, year, yearly_revenue, last_year_revenue, and revenue_change.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt