Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Period-over-Period Comparisons (MoM, YoY) | Analytical SQL Techniques
SQL for Analytical Reports
Seção 1. Capítulo 4
single

single

bookPeriod-over-Period Comparisons (MoM, YoY)

Deslize para mostrar o 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
Tarefa

Deslize para começar a programar

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt