pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date
Date: Thu, 22 Feb 2018 21:45:51 -0800
Message-ID: <cea3b04d-0e59-fb5a-131b-6934d2e6f0f6@illuminatedcomputing.com> (raw)
In-Reply-To: <CAD3a31WKAkzHGaoXygSoc3pL9ouPW5v1f8XFg404AjwVDKHOYQ@mail.gmail.com>
References: <CAD3a31WKAkzHGaoXygSoc3pL9ouPW5v1f8XFg404AjwVDKHOYQ@mail.gmail.com>

On 02/22/2018 04:44 PM, Ken Tanzer wrote:
> Hi, hoping to get some help with this.  I'm needing to take a specific 
> date, a series of dateranges  and, given a specific date, return a 
> single conitinuous daterange that includes that date.

The part about joining multiple touching dateranges to give a single 
continuous daterange is what Richard Snodgrass calls "coalescing" in 
*Developing Time-Oriented Database Applications in SQL*, pages 159 - 
169, available printed or as a free PDF at 
http://www2.cs.arizona.edu/~rts/publications.html (His approach also 
supports overlapping ranges, but it sounds like you don't need that.) If 
you had a coalesced view (or maybe a set-returning function), you could 
do this:

SELECT  term
FROM    coalesced_staff_assign
WHERE   client_id = 5
AND     term @> '2018-15-01'
;

I can't think of any way to avoid scanning all of a given client's 
records, but hopefully client_id alone would be selective enough to 
still give you good performance.

Oh also: in reading Snodgrass's SQL, note that he assumes closed-open 
ranges (i.e. '[)'), so you'll need to adjust some things to fit with 
your closed-closed ranges (or always use `staff_assign_date_end - 
INTERVAL '1 day'` if every assignment is at least 1 day long). On the 
other hand with built-in range types you might be able to simplify his 
pure-SQL solutions.

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com




view thread (18+ messages)  latest in thread

Message-ID: <cea3b04d-0e59-fb5a-131b-6934d2e6f0f6@illuminatedcomputing.com>
Permalink:  ../cea3b04d-0e59-fb5a-131b-6934d2e6f0f6@illuminatedcomputing.com/
Also on:    postgresql.org/message-id/cea3b04d-0e59-fb5a-131b-6934d2e6f0f6@illuminatedcomputing.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pj@illuminatedcomputing.com
  Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date
  In-Reply-To: <cea3b04d-0e59-fb5a-131b-6934d2e6f0f6@illuminatedcomputing.com>
  References: <CAD3a31WKAkzHGaoXygSoc3pL9ouPW5v1f8XFg404AjwVDKHOYQ@mail.gmail.com> <cea3b04d-0e59-fb5a-131b-6934d2e6f0f6@illuminatedcomputing.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox