Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Time Series Analysis: Gaps and Islands | Analytical SQL Techniques
SQL for Analytical Reports
Sección 1. Capítulo 14
single

single

bookTime Series Analysis: Gaps and Islands

Desliza para mostrar el menú

Time series analysis is essential in analytical reporting, particularly when you need to identify patterns of activity and inactivity over a sequence of dates. One of the most practical challenges in time series data is detecting gaps and islands. These concepts help you find periods where events are missing (gaps) or group together continuous stretches of activity (islands).

Note
Definition
  • Gaps are stretches of time where no records exist for an expected event (such as no sales on certain days);
  • Islands are consecutive periods where events are present without interruption (such as a streak of daily sales or employee absences).

Significance:
Detecting gaps and islands helps you answer business-critical questions:

  • Are there days with no sales activity that require investigation?;
  • How long do continuous employee absences last, and are there patterns?;
  • Did marketing campaigns run without interruption, or are there periods of inactivity?.

Understanding these periods is crucial for operational monitoring, compliance, and performance improvement.

123456789
-- Find all dates in January 2024 where no sales were recorded in any store WITH calendar AS ( SELECT generate_series('2024-01-01'::date, '2024-01-31'::date, interval '1 day') AS sale_date ) SELECT c.sale_date FROM calendar c LEFT JOIN sales s ON c.sale_date = s.sale_date WHERE s.sale_id IS NULL ORDER BY c.sale_date;
copy

The retail operations team often needs to pinpoint days when no sales were recorded in any store. This helps them investigate potential issues with staffing, inventory, or demand. By comparing a generated calendar to the actual sales records, you can efficiently detect these gaps and take action.

123456789
-- Detect gaps in daily sales data across all stores for Q1 2024 WITH calendar AS ( SELECT generate_series('2024-01-01'::date, '2024-03-31'::date, interval '1 day') AS sale_date ) SELECT c.sale_date FROM calendar c LEFT JOIN sales s ON c.sale_date = s.sale_date WHERE s.sale_id IS NULL ORDER BY c.sale_date;
copy
Tarea

Desliza para comenzar a programar

Write a query to find continuous periods of employee absence.

  • Identify each employee's absences as a sequence of dates.
  • Assign a group to each continuous absence period for each employee.
  • For each group, return the employee ID, the start and end date of the absence period, and the number of days absent.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 14
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt