single
Period-over-Period Comparisons (MoM, YoY)
Swipe um das Menü anzuzeigen
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.
- 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.
12345678SELECT 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;
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.
12345678910111213SELECT 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;
Wischen, um mit dem Codieren zu beginnen
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, andrevenue_change.
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen