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

single

bookTime Series Analysis: Gaps and Islands

Veeg om het menu te tonen

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
Taak

Veeg om te beginnen met coderen

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 14
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt