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

single

bookTime Series Analysis: Gaps and Islands

Swipe um das Menü anzuzeigen

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
Aufgabe

Wischen, um mit dem Codieren zu beginnen

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ösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 14
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt