Re: Finding date intersections

From: David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Finding date intersections
Date: 2014-10-24 18:02:59
Message-ID: 1414173779425-5824194.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gaurav Kumar 2014-10-24 18:13:05 "can not able to find scan Function For making NoDB"
Previous Message Oliver Kohll - Mailing Lists 2014-10-24 16:30:35 dblink password required