Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Time Series Analysis: Gaps and Islands | Analytical SQL Techniques
SQL for Analytical Reports
Avsnitt 1. Kapitel 14
single

single

bookTime Series Analysis: Gaps and Islands

Svep för att visa menyn

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
Uppgift

Svep för att börja koda

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.

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 14
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