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

single

bookPeriod-over-Period Comparisons (MoM, YoY)

Svep för att visa menyn

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
Uppgift

Svep för att börja koda

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt