Re: Finding date intersections - Mailing list pgsql-general

From David G Johnston
Subject Re: Finding date intersections
Date
Msg-id 1414173779425-5824194.post@n5.nabble.com
Whole thread Raw
In response to Re: Finding date intersections  (John McKown <john.archie.mckown@gmail.com>)
Responses Re: Finding date intersections  (Joe Van Dyk <joe@tanga.com>)
List pgsql-general
John McKown wrote
>> insert into sales values
>>   (tstzrange('2014-1-1', '2014-1-2')),
>>   (tstzrange('2014-1-2', '2014-1-3')),
>>   (tstzrange('2014-1-2', '2014-1-4')),
>>   (tstzrange('2014-1-5', '2014-1-6'));
>>
>> -- want back:
>> --   tstzrange('2014-1-1', '2014-1-4')
>> --   tstzrange('2014-1-6', '2014-1-6')
>>

I presume the second output row should be [5,6)...

And why are you using a timestamp range when your data are dates?

My first thought is to explode the ranges into distinct dates, order them
inside a window, use lag(...) to find breaks,p and assign groups, the for
each group take the min and max of the group and form a new range.  Not sure
exactly what the SQL looks like - especially the range explosion - but
should technically work even though performance may suck. Probably want to
use lateral and generate_series(...) if you are on a more recent version.

David J.





--
View this message in context: http://postgresql.1045698.n5.nabble.com/Finding-date-intersections-tp5824102p5824194.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Oliver Kohll - Mailing Lists
Date:
Subject: dblink password required
Next
From: Joe Van Dyk
Date:
Subject: Re: Finding date intersections