I have a table in which I have an ID binding:
ββββββββββββββββββ¬βββββββββββββββββββββββ
β location_id | datetime |
ββββββββββββββββββΌβββββββββββββββββββββββ€
β 200333 | 2008-01-01 00:00:00 |
β 200333 | 2008-01-01 01:00:00 |
β 200333 | 2008-01-01 02:00:00 |
| ... | ... |
β 200333 | 2009-10-23 21:00:00 |
β 200333 | 2009-10-23 22:00:00 |
β 200333 | 2009-10-23 23:00:00 |
β 200768 | 2008-06-01 00:00:00 |
β 200768 | 2008-06-01 01:00:00 |
β 200768 | 2008-06-01 02:00:00 |
| ... | ... |
β 200768 | 2009-12-31 00:00:00 |
β 200768 | 2009-12-31 00:00:00 |
β 200768 | 2009-12-31 00:00:00 |
ββββββββββββββββββ΄βββββββββββββββββββββββ
How can I choose the longest time period for these two overlapping sections location_id? In this case, the desired output would be:
ββββββββββββββββββββββββ¬βββββββββββββββββββββββ
β start | end |
ββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β 2008-06-01 00:00:00 | 2009-10-23 23:00:00 |
ββββββββββββββββββββββββ΄βββββββββββββββββββββββ
I can easily get the longest period using MIN()and MAX(), but how would I like to choose the maximum minimum time and minimum maximum dates?
Oh, and this table contains 19 million rows, so bonus points for offers that are quickly executed :)
source
share