Skip to content

Instantly share code, notes, and snippets.

@agraves
Created May 30, 2012 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agraves/2839143 to your computer and use it in GitHub Desktop.
Save agraves/2839143 to your computer and use it in GitHub Desktop.
Date range overlaps
-- Calculate the number of days within a range overlapped by an arbitrary number of other ranges.
SELECT
COUNT(active_day),
action_id
FROM (
SELECT
DISTINCT(
GENERATE_SERIES(
start_at,
finish_at,
INTERVAL '1 day'
)::date
) AS active_day,
action_id
FROM score_events
WHERE (
(start_at, finish_at) OVERLAPS (
DATE('2011-01-01'),
DATE('2011-01-06 06:00:01 UTC')
)
)
) AS active_day_series
WHERE active_day BETWEEN '2011-01-01' AND '2011-01-06 06:00:01 UTC'
GROUP BY action_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment