Complete worked example
sql
WITH calendar(day,n) AS (VALUES ('2026-10-01',1),('2026-10-02',2),('2026-10-03',3),('2026-10-04',4)),
present AS (SELECT DISTINCT r.published AS day,c.n FROM roles r JOIN calendar c ON c.day=r.published WHERE r.company_id='A'),
marked AS (SELECT day,n,CASE WHEN n-LAG(n) OVER (ORDER BY n)=1 THEN 0 ELSE 1 END AS start FROM present)
SELECT day,SUM(start) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS island
FROM marked ORDER BY day;- Reasoning
- A has two distinct publication days separated by a calendar gap.