pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Viktor Fougstedt <viktor@chalmers.se>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date
Date: Fri, 23 Feb 2018 14:42:19 +0000
Message-ID: <8B485EC3-C485-4313-9758-B145FDA59B19@chalmers.se> (raw)
In-Reply-To: <CAD3a31WKAkzHGaoXygSoc3pL9ouPW5v1f8XFg404AjwVDKHOYQ@mail.gmail.com>
References: <CAD3a31WKAkzHGaoXygSoc3pL9ouPW5v1f8XFg404AjwVDKHOYQ@mail.gmail.com>


Hi.

This answer is perhaps useful if I understand your problem correctly. But I might have interpreted it wrongly. :-)


I would probably start with merging intervals so that overlapping and adjacent intervals become single continuous intervals, then select from those merged intervals.

We have an application with a lot of interval handling in PostgreSQL, and we use many variants of algorithms based on one by Itzik Ben Gan which he calls “packing intervals”. The post we started with was an old reader’s challenge from SQL Magazine. It has since been updated when MS-SQL started supporting window functions better (Itzik Ben Gan is a MS-SQL-guy).

Basically, it is a few CTE:s which convert the intervals into “start” (+1) and “stop” (-1) events, then keeps a running sum of these, and finally creates new intervals by noting that the merged (or “packed”) intervals starts with events that had sum=0 before them and stops with events which have sum=0 after them.

It involves both CTE:s and window functions and is quite a beautiful example of SQL, IMHO.

I think it’s best to google it, he does a good job of explaining how it works.

Hope that helps a bit at least.

/Viktor

On 23 Feb 2018, at 01:44, Ken Tanzer <ken.tanzer@gmail.com<mailto:ken.tanzer@gmail.com>> 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.

To elaborate a bit, I've got lots of tables that include start and end dates.  For example:

CREATE TABLE tbl_staff_assign (
    staff_assign_id         SERIAL PRIMARY KEY,
    client_id               INTEGER NOT NULL REFERENCES tbl_client (client_id),
    staff_id                INTEGER REFERENCES tbl_staff(staff_id),
    staff_assign_type_code      VARCHAR(10) NOT NULL REFERENCES tbl_l_staff_assign_type (staff_assign_type_code),
    staff_assign_date           DATE NOT NULL,
    staff_assign_date_end       DATE,
...

So a client might leave a progrma and then return later, or they might simply switch to another staff_id.  (In which case one record will have and end date, and the next record will start on the next day.)  In this case I need to know "what period were they continuously in the program that includes X date?"  So I'd like to be able to do something like:

"SELECT staff_assign_date,continuous_daterange( staff_assign_date, (SELECT array_agg(daterange(staff_assign_date,staff_assign_date_end,'[]')
) FROM staff_assign sa2 WHERE sa2.client_id=sa.client_id) FROM staff_assign sa

I've done this before with procedures specific to a particular table, and working with the start and end dates.  I'm now wanting to try to do this once generically that will work for all my cases.  So I'm hoping to do this in a way that performance isn't horrible.  And it's a little unclear to me how much and how I might be able to use the daterange operators to accomplish this efficiently.

Any advice or suggestions or ways to go about this appreciated.  Thanks!

Ken

p.s.,  Another small wrinkle is these records aren't always perfect, and ideally I'd allow for an optional fudge factor that would allow small gaps to be ignored.  I could just add that in every query (start_date+2,end_date-2), but it might be nice to have the function do it, if it didn't badly hurt performance.


--
[http://agency-software.org/demo/client/images/agency_logo_small.png]
AGENCY Software
A Free Software data system
By and for non-profits
http://agency-software.org/
https://demo.agency-software.org/client
ken.tanzer@agency-software.org<mailto:ken.tanzer@agency-software.org>
(253) 245-3801

Subscribe to the mailing list<mailto:agency-general-request@lists.sourceforge.net?body=subscribe> to
learn more about AGENCY or
follow the discussion.



view thread (18+ messages)  latest in thread

Message-ID: <8B485EC3-C485-4313-9758-B145FDA59B19@chalmers.se>
Permalink:  ../8B485EC3-C485-4313-9758-B145FDA59B19@chalmers.se/
Also on:    postgresql.org/message-id/8B485EC3-C485-4313-9758-B145FDA59B19@chalmers.se

 · 

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: viktor@chalmers.se, ken.tanzer@gmail.com
  Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date
  In-Reply-To: <8B485EC3-C485-4313-9758-B145FDA59B19@chalmers.se>
  References: <CAD3a31WKAkzHGaoXygSoc3pL9ouPW5v1f8XFg404AjwVDKHOYQ@mail.gmail.com> <8B485EC3-C485-4313-9758-B145FDA59B19@chalmers.se>

* 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