Created
May 30, 2012 21:45
-
-
Save agraves/2839143 to your computer and use it in GitHub Desktop.
Date range overlaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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