pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Given a set of daterange, finding the continuous range that includes a particular date
18+ messages / 5 participants
[nested] [flat]

* Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 00:44  Ken Tanzer <ken.tanzer@gmail.com>
  0 siblings, 3 replies; 18+ messages in thread

From: Ken Tanzer @ 2018-02-23 00:44 UTC (permalink / raw)
  To: pgsql-general

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.


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

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

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 00:53  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  2 siblings, 1 reply; 18+ messages in thread

From: Adrian Klaver @ 2018-02-23 00:53 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; pgsql-general

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.
> 
> 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.

The operator I use to solve similar problems:

https://www.postgresql.org/docs/10/static/functions-range.html

@> 	contains element 	'[2011-01-01,2011-03-01)'::tsrange @> 
'2011-01-10'::timestamp 	t


> 
> 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.
> 
> 
> -- 
> 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.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 00:58  Ken Tanzer <ken.tanzer@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Ken Tanzer @ 2018-02-23 00:58 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-general

On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> 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.
>>
>> 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.
>>
>
> The operator I use to solve similar problems:
>
> https://www.postgresql.org/docs/10/static/functions-range.html
>
> @>      contains element        '[2011-01-01,2011-03-01)'::tsrange @>
> '2011-01-10'::timestamp   t
>
>
Thanks Adrian.  But how would you apply that to this situation, where I
have a series of (quite possibly discontinuous) dateranges?


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

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

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 01:05  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Adrian Klaver @ 2018-02-23 01:05 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general

On 02/22/2018 04:58 PM, Ken Tanzer wrote:
> 
> 
> On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     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.
> 
>         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.
> 
> 
>     The operator I use to solve similar problems:
> 
>     https://www.postgresql.org/docs/10/static/functions-range.html
>     <https://www.postgresql.org/docs/10/static/functions-range.html;
> 
>     @>      contains element        '[2011-01-01,2011-03-01)'::tsrange
>     @> '2011-01-10'::timestamp   t
> 
> 
> Thanks Adrian.  But how would you apply that to this situation, where I 
> have a series of (quite possibly discontinuous) dateranges?

This is going to depend on a more formal definition of the problem with 
some sample data. Right now I am trying to reconcile "what period were 
they continuously in the program that includes X date?" with "where I 
have a series of (quite possibly discontinuous) dateranges? " Maybe its 
just me,  I can't see how discontinuous can also be continuously.

> 
> 
> -- 
> 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.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 01:23  Ken Tanzer <ken.tanzer@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 3 replies; 18+ messages in thread

From: Ken Tanzer @ 2018-02-23 01:23 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-general

On Thu, Feb 22, 2018 at 5:05 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 02/22/2018 04:58 PM, Ken Tanzer wrote:
>
>
>>
>> On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver <adrian.klaver@aklaver.com
>> <mailto:adrian.klaver@aklaver.com>> wrote:
>>
>>     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.
>>
>>         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.
>>
>>
>>     The operator I use to solve similar problems:
>>
>>     https://www.postgresql.org/docs/10/static/functions-range.html
>>     <https://www.postgresql.org/docs/10/static/functions-range.html;
>>
>>     @>      contains element        '[2011-01-01,2011-03-01)'::tsrange
>>     @> '2011-01-10'::timestamp   t
>>
>>
>> Thanks Adrian.  But how would you apply that to this situation, where I
>> have a series of (quite possibly discontinuous) dateranges?
>>
>
> This is going to depend on a more formal definition of the problem with
> some sample data. Right now I am trying to reconcile "what period were they
> continuously in the program that includes X date?" with "where I have a
> series of (quite possibly discontinuous) dateranges? " Maybe its just me,
> I can't see how discontinuous can also be continuously.
>
>
>>
*Start   End*
-- Episode 1
1/1/16  3/30/16
4/1/16  4/30/16
-- Episode 2
1/1/18  1/31/18
2/1/18  NULL

Given 1/15/18, should return 1/1/18-->NULL
Given 1/15/16, should return 1/1/16-->4/30/16

Thinking about this a bit more, I'm wondering if a window function could be
used for this?  I've used them a bit, but definitely never wrote one.
Something like

continuous_daterange(staff_assign_date,daterange(staff_assign_date,staff_assign_date_end,'[[]')
OVER (PARTION BY client_id)

But then a window function can only see the rows included in the query,
yes?  In which case this would only work if you queried for all the rows
for a particular client_id?  I guess in the example I gave I was doing that
anyway, so maybe this would be no worse.

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

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

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 01:47  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  2 siblings, 0 replies; 18+ messages in thread

From: Adrian Klaver @ 2018-02-23 01:47 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general

On 02/22/2018 05:23 PM, Ken Tanzer wrote:
> 
> 
> On Thu, Feb 22, 2018 at 5:05 PM, Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 02/22/2018 04:58 PM, Ken Tanzer wrote:
> 
> 
> 
>         On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver
>         <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>         <mailto:adrian.klaver@aklaver.com
>         <mailto:adrian.klaver@aklaver.com>>> wrote:
> 
>              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.
> 
>                  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.
> 
> 
>              The operator I use to solve similar problems:
> 
>         https://www.postgresql.org/docs/10/static/functions-range.html
>         <https://www.postgresql.org/docs/10/static/functions-range.html;
>             
>         <https://www.postgresql.org/docs/10/static/functions-range.html
>         <https://www.postgresql.org/docs/10/static/functions-range.html>;
> 
>              @>      contains element       
>         '[2011-01-01,2011-03-01)'::tsrange
>              @> '2011-01-10'::timestamp   t
> 
> 
>         Thanks Adrian.  But how would you apply that to this situation,
>         where I have a series of (quite possibly discontinuous) dateranges?
> 
> 
>     This is going to depend on a more formal definition of the problem
>     with some sample data. Right now I am trying to reconcile "what
>     period were they continuously in the program that includes X date?"
>     with "where I have a series of (quite possibly discontinuous)
>     dateranges? " Maybe its just me,  I can't see how discontinuous can
>     also be continuously.
> 
> 
> 
> *Start   End*
> -- Episode 1
> 1/1/16  3/30/16
> 4/1/16  4/30/16
> -- Episode 2
> 1/1/18  1/31/18
> 2/1/18  NULL
> 
> Given 1/15/18, should return 1/1/18-->NULL

I have similar case. There I use the coalesce to substitute the date the 
report is run for the NULL value, mainly because I have not way of 
predicting the future:)

Is there actually an episode value you can sort by or is that a made up 
value for this example?


> Given 1/15/16, should return 1/1/16-->4/30/16
> 
> Thinking about this a bit more, I'm wondering if a window function could 
> be used for this?  I've used them a bit, but definitely never wrote one. 
>    Something like
> 
> continuous_daterange(staff_assign_date,daterange(staff_assign_date,staff_assign_date_end,'[[]') 
> OVER (PARTION BY client_id)
> 
> But then a window function can only see the rows included in the query, 
> yes?  In which case this would only work if you queried for all the rows 
> for a particular client_id?  I guess in the example I gave I was doing 
> that anyway, so maybe this would be no worse.
> 
> Ken
> -- 
> 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.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 05:05  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  2 siblings, 1 reply; 18+ messages in thread

From: Adrian Klaver @ 2018-02-23 05:05 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general

On 02/22/2018 05:23 PM, Ken Tanzer wrote:
> 
> 
> On Thu, Feb 22, 2018 at 5:05 PM, Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 02/22/2018 04:58 PM, Ken Tanzer wrote:
> 
> 
> 
>         On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver
>         <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>         <mailto:adrian.klaver@aklaver.com
>         <mailto:adrian.klaver@aklaver.com>>> wrote:
> 
>              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.
> 
>                  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.
> 
> 
>              The operator I use to solve similar problems:
> 
>         https://www.postgresql.org/docs/10/static/functions-range.html
>         <https://www.postgresql.org/docs/10/static/functions-range.html;
>             
>         <https://www.postgresql.org/docs/10/static/functions-range.html
>         <https://www.postgresql.org/docs/10/static/functions-range.html>;
> 
>              @>      contains element       
>         '[2011-01-01,2011-03-01)'::tsrange
>              @> '2011-01-10'::timestamp   t
> 
> 
>         Thanks Adrian.  But how would you apply that to this situation,
>         where I have a series of (quite possibly discontinuous) dateranges?
> 
> 
>     This is going to depend on a more formal definition of the problem
>     with some sample data. Right now I am trying to reconcile "what
>     period were they continuously in the program that includes X date?"
>     with "where I have a series of (quite possibly discontinuous)
>     dateranges? " Maybe its just me,  I can't see how discontinuous can
>     also be continuously.
> 
> 
> 
> *Start   End*
> -- Episode 1
> 1/1/16  3/30/16
> 4/1/16  4/30/16
> -- Episode 2
> 1/1/18  1/31/18
> 2/1/18  NULL
> 
> Given 1/15/18, should return 1/1/18-->NULL
> Given 1/15/16, should return 1/1/16-->4/30/16

Just thinking out loud here, in a function:

1) For a client_id you can find the min(staff_assign_date).

2) You can create a max(staff_assign_date_end) by using 
COALESCE(staff_assign_date_end, current_date)

3) You now have the outer range for the episodes.

4) In that range of dates for each staff_assign_date you find the 
staff_assign_date_end that greater then the staff_assign_date but less 
then the next staff_assign_date. That will give you your episodes.

5) You can then test to see if the X date is in the calculated episodes.

6) As a shortcut you can find min(staff_assign_date) that is less then 
the X date and see if the staff_assign_date_end is greater then the X date.

> 
> Thinking about this a bit more, I'm wondering if a window function could 
> be used for this?  I've used them a bit, but definitely never wrote one. 
>    Something like
> 
> continuous_daterange(staff_assign_date,daterange(staff_assign_date,staff_assign_date_end,'[[]') 
> OVER (PARTION BY client_id)
> 
> But then a window function can only see the rows included in the query, 
> yes?  In which case this would only work if you queried for all the rows 
> for a particular client_id?  I guess in the example I gave I was doing 
> that anyway, so maybe this would be no worse.
> 
> Ken
> -- 
> 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.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 05:08  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 18+ messages in thread

From: Adrian Klaver @ 2018-02-23 05:08 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general

On 02/22/2018 09:05 PM, Adrian Klaver wrote:
> On 02/22/2018 05:23 PM, Ken Tanzer wrote:
>>
>>
>> On Thu, Feb 22, 2018 at 5:05 PM, Adrian Klaver 
>> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
>>
>>     On 02/22/2018 04:58 PM, Ken Tanzer wrote:
>>
>>
>>
>>         On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver
>>         <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>>         <mailto:adrian.klaver@aklaver.com
>>         <mailto:adrian.klaver@aklaver.com>>> wrote:
>>
>>              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.
>>
>>                  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.
>>
>>
>>              The operator I use to solve similar problems:
>>
>>         https://www.postgresql.org/docs/10/static/functions-range.html
>>         <https://www.postgresql.org/docs/10/static/functions-range.html;
>>         <https://www.postgresql.org/docs/10/static/functions-range.html
>>         <https://www.postgresql.org/docs/10/static/functions-range.html>;
>>
>>              @>      contains element         
>> '[2011-01-01,2011-03-01)'::tsrange
>>              @> '2011-01-10'::timestamp   t
>>
>>
>>         Thanks Adrian.  But how would you apply that to this situation,
>>         where I have a series of (quite possibly discontinuous) 
>> dateranges?
>>
>>
>>     This is going to depend on a more formal definition of the problem
>>     with some sample data. Right now I am trying to reconcile "what
>>     period were they continuously in the program that includes X date?"
>>     with "where I have a series of (quite possibly discontinuous)
>>     dateranges? " Maybe its just me,  I can't see how discontinuous can
>>     also be continuously.
>>
>>
>>
>> *Start   End*
>> -- Episode 1
>> 1/1/16  3/30/16
>> 4/1/16  4/30/16
>> -- Episode 2
>> 1/1/18  1/31/18
>> 2/1/18  NULL
>>
>> Given 1/15/18, should return 1/1/18-->NULL
>> Given 1/15/16, should return 1/1/16-->4/30/16
> 
> Just thinking out loud here, in a function:
> 
> 1) For a client_id you can find the min(staff_assign_date).
> 
> 2) You can create a max(staff_assign_date_end) by using 
> COALESCE(staff_assign_date_end, current_date)
> 
> 3) You now have the outer range for the episodes.
> 
> 4) In that range of dates for each staff_assign_date you find the 
> staff_assign_date_end that greater then the staff_assign_date but less 
> then the next staff_assign_date. That will give you your episodes.
> 
> 5) You can then test to see if the X date is in the calculated episodes.
> 
> 6) As a shortcut you can find min(staff_assign_date) that is less then 
> the X date and see if the staff_assign_date_end is greater then the X date.
> 

Correction to 6)

6) As a shortcut you can find max(staff_assign_date) that is less then 
the X date and see if the staff_assign_date_end is greater then the X date.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 05:45  Paul Jungwirth <pj@illuminatedcomputing.com>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  2 siblings, 0 replies; 18+ messages in thread

From: Paul Jungwirth @ 2018-02-23 05:45 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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




^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 14:10  mariusz <marius@mtvk.pl>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  2 siblings, 0 replies; 18+ messages in thread

From: mariusz @ 2018-02-23 14:10 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

On Thu, 2018-02-22 at 17:23 -0800, Ken Tanzer wrote:
> 
> 
> On Thu, Feb 22, 2018 at 5:05 PM, Adrian Klaver
> <adrian.klaver@aklaver.com> wrote:
>         On 02/22/2018 04:58 PM, Ken Tanzer wrote:
>         
>                 
>                 
>                 On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver
>                 <adrian.klaver@aklaver.com
>                 <mailto:adrian.klaver@aklaver.com>> wrote:
>                 
>                     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.
>                 
>                         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.
>                 
>                 
>                     The operator I use to solve similar problems:
>                 
>                 
>                 https://www.postgresql.org/docs/10/static/functions-range.html
>                 
>                 <https://www.postgresql.org/docs/10/static/functions-range.html;
>                 
>                     @>      contains element
>                 '[2011-01-01,2011-03-01)'::tsrange
>                     @> '2011-01-10'::timestamp   t
>                 
>                 
>                 Thanks Adrian.  But how would you apply that to this
>                 situation, where I have a series of (quite possibly
>                 discontinuous) dateranges?
>         
>         
>         This is going to depend on a more formal definition of the
>         problem with some sample data. Right now I am trying to
>         reconcile "what period were they continuously in the program
>         that includes X date?" with "where I have a series of (quite
>         possibly discontinuous) dateranges? " Maybe its just me,  I
>         can't see how discontinuous can also be continuously.
>         
>                 
> 
> 
> Start   End
> -- Episode 1
> 1/1/16  3/30/16
> 4/1/16  4/30/16
> -- Episode 2
> 
> 1/1/18  1/31/18
> 2/1/18  NULL
> 
> 
> Given 1/15/18, should return 1/1/18-->NULL
> Given 1/15/16, should return 1/1/16-->4/30/16
> 


i guess, you can easily get max continuous range for each row with
something like this:

CREATE OR REPLACE FUNCTION
        append_daterange(d1 daterange, d2 daterange)
        RETURNS daterange
        LANGUAGE sql
        AS
$$
        SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2 END;
$$;

CREATE AGGREGATE agg_daterange (daterange) (
        sfunc = append_daterange,
        stype = daterange
);

SELECT dr,
       lower(agg_daterange(dr) OVER (ORDER BY dr ASC)),
       upper(agg_daterange(dr) OVER (ORDER BY dr DESC))
  FROM ...

above example is simplified to selecting only daterange column "dr" for
readability, which in your case should be something like

daterange(staff_assign_date,staff_assign_date_end,'[)')

please note that daterange would be normalized to [) format so upper()
above returns exactly your max "staff_assign_date_end" for each
continuous range when dateranges are created with '[)' format.

the key point is ... ELSE d2 in append_daterange() which starts with new
value each time that new value is discontinuous with agg's state value
and order in which rows are processed (ASC for lower of daterange, DESC
for upper of daterange).

unfortunately this involves reading all rows for "client_id" and
additional sorting for each window.
i recall reading that you already pay the price of reading all rows for
client_id anyway, so the only question is the cost of two additional
sorts (maybe extracting dateranges to subset on which to do windows and
rejoining result of continuous ranges to original set would help to
lower the cost).

another way would be recursive cte, easy to append continuous dateranges
one by one from a set of dateranges staring with some date of choice,
but i doubt it would be cheaper, not to mention additional
postprocessing to get what you really want. and definitely would be less
readable.


i guess, from that point where for each row you have start and end date
of max continuous range including given row, you can easily format or
filter whatever you want


> Thinking about this a bit more, I'm wondering if a window function
> could be used for this?  I've used them a bit, but definitely never
> wrote one.   Something like 
> 
> 
> continuous_daterange(staff_assign_date,daterange(staff_assign_date,staff_assign_date_end,'[[]') OVER (PARTION BY client_id)
> 
> 
> But then a window function can only see the rows included in the
> query, yes?  In which case this would only work if you queried for all
> the rows for a particular client_id?  I guess in the example I gave I
> was doing that anyway, so maybe this would be no worse.
> 
> 
> Ken
> -- 
> 
> 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
> (253) 245-3801
> 
> 
> Subscribe to the mailing list to
> learn more about AGENCY or
> follow the discussion.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-23 14:42  Viktor Fougstedt <viktor@chalmers.se>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  2 siblings, 1 reply; 18+ messages in thread

From: Viktor Fougstedt @ 2018-02-23 14:42 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general


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.



^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date
@ 2018-02-24 01:07  Ken Tanzer <ken.tanzer@gmail.com>
  parent: Viktor Fougstedt <viktor@chalmers.se>
  0 siblings, 0 replies; 18+ messages in thread

From: Ken Tanzer @ 2018-02-24 01:07 UTC (permalink / raw)
  To: Viktor Fougstedt <viktor@chalmers.se>; +Cc: pgsql-general

On Fri, Feb 23, 2018 at 6:42 AM, Viktor Fougstedt <viktor@chalmers.se>
wrote:

>
> 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
>
> Hi.  Thanks for the many helpful responses!  Although I didn't end up with
exactly what I was looking for, I think I got to something that works good
enough for now.  I did it with CTEs, in a way I think similar to what you
were suggesting (my printer is out of ink, so I didn't actually get to look
at that book yet!)  I ended up having to do 4 passes:

1) Identify the starts and ends of continuous ranges
2) Eliminate the middle-point records (non stops/starts)
3) Merge the stop date in with the starts
4) Eliminate the stops

I couldn't see how to do it in less steps, but if there's a way...

I posted the query along with some sample data below.

Thanks again!

Ken

BEGIN;

CREATE TEMP TABLE sample_data (
client_id INTEGER,
start_date DATE,
end_date DATE,
episode INTEGER -- I won't actually have this info; just a label I added
for clarity and testing
);

INSERT INTO sample_data VALUES


(1,'1990-01-01','1990-12-31',0),
(1,'1991-01-01','1991-12-31',0),
(1,'1995-01-01','1995-06-30',1),
(1,'2000-01-01','2000-12-31',2),
(1,'2001-01-01','2001-12-31',2),
(1,'2002-01-01','2002-12-31',2),
(1,'2003-01-01','2003-12-31',2),
(1,'2004-01-01','2004-12-31',2),
(1,'2005-01-01','2005-12-31',2),
(1,'2006-01-01','2006-12-31',2),
(1,'2014-01-01','2014-12-31',3),
(1,'2015-01-01','2015-12-31',3),
(1,'2017-06-30','2017-12-31',4),
(1,'2018-01-01',NULL,4),

(2,'2014-02-01','2015-01-31',0),
(2,'2015-02-01','2015-12-31',0),
(2,'2017-09-30','2018-01-31',1),
(2,'2018-02-01','2018-02-14',1)
;

WITH peek3 AS (
WITH peek2 AS (
WITH peek AS (
SELECT
  client_id,
  episode,
  daterange(start_date,end_date,'[]') AS range,
  COALESCE(daterange(start_date,end_date+1,'[]') &&
  lead(daterange(start_date,end_date,'[]')) OVER (PARTITION BY client_id
ORDER BY start_date),false) AS continues,
  COALESCE(daterange(start_date,end_date,'[]') &&
  lag(daterange(start_date,end_date+1,'[]')) OVER (PARTITION BY client_id
ORDER BY start_date),false) AS is_continued
FROM
  sample_data
)
SELECT
  *
FROM peek
  WHERE NOT (is_continued AND continues)
)
SELECT client_id,episode,range,
daterange(lower(range),
  CASE WHEN lead(is_continued) OVER (PARTITION BY client_id ORDER BY range)
THEN
  lead(upper(range)) OVER (PARTITION BY client_id ORDER BY range)
  ELSE upper(range)
  END) AS full_range
,continues,is_continued
FROM peek2
)
SELECT * FROM peek3
WHERE NOT is_continued
;



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

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

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates)
@ 2018-02-24 02:11  Ken Tanzer <ken.tanzer@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Ken Tanzer @ 2018-02-24 02:11 UTC (permalink / raw)
  To: mariusz <marius@mtvk.pl>; +Cc: pgsql-general

>
> On Fri, Feb 23, 2018 at 6:10 AM, mariusz <marius@mtvk.pl> wrote:


> i guess, you can easily get max continuous range for each row with
> something like this:
>
> CREATE OR REPLACE FUNCTION
>         append_daterange(d1 daterange, d2 daterange)
>         RETURNS daterange
>         LANGUAGE sql
>         AS
> $$
>         SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2 END;
> $$;
>
> CREATE AGGREGATE agg_daterange (daterange) (
>         sfunc = append_daterange,
>         stype = daterange
> );
>
> SELECT dr,
>        lower(agg_daterange(dr) OVER (ORDER BY dr ASC)),
>        upper(agg_daterange(dr) OVER (ORDER BY dr DESC))
>   FROM ...
>
> above example is simplified to selecting only daterange column "dr" for
> readability, which in your case should be something like
>
> daterange(staff_assign_date,staff_assign_date_end,'[)')
>
> please note that daterange would be normalized to [) format so upper()
> above returns exactly your max "staff_assign_date_end" for each
> continuous range when dateranges are created with '[)' format.
>
> the key point is ... ELSE d2 in append_daterange() which starts with new
> value each time that new value is discontinuous with agg's state value
> and order in which rows are processed (ASC for lower of daterange, DESC
> for upper of daterange).
>
> unfortunately this involves reading all rows for "client_id" and
> additional sorting for each window.
> i recall reading that you already pay the price of reading all rows for
> client_id anyway, so the only question is the cost of two additional
> sorts (maybe extracting dateranges to subset on which to do windows and
> rejoining result of continuous ranges to original set would help to
> lower the cost).
>
>
Thank you, and I wanted to follow up on this.  I couldn't quite get your
example working as described, but I also ended up trying something very
similar that got me very close but not quite there.  Basically, I can see
that it is correctly calculating the ranges (in the notices), but it is
only returning the last range for each client.  (Because I said PARTITION
BY client_id).

So I'm not sure if I should be calling this differently, or if the function
needs to work differently, or if this just isn't possible.  Do I need to
partition by something else, and if so what?  I don't see what I could
specify that would indicate a new episode.

Also, it's not clear to me how an aggregate might define/return different
values within a partition.  Although this must be possible, since functions
like rank() and row_number() seem to do it.

Hoping there is something easy that can be tweaked here.  See below for
copy/pastable test stuff.  It includes output from both functions.  Both
look to be returning the same results, which makes me wonder if my passing
in a start date was a waste of time, though it seems to me it would be
necessary.

Cheers,
Ken

BEGIN;
CREATE TEMP TABLE sample_data (
client_id INTEGER,
start_date DATE,
end_date DATE,
episode INTEGER -- Just a label, for clarity
);

INSERT INTO sample_data VALUES
(1,'1990-01-01','1990-12-31',1),
(1,'1991-01-01','1991-12-31',1),

(1,'2000-01-01','2000-12-31',2),
(1,'2001-01-01','2001-12-31',2),
(1,'2002-01-01','2002-12-31',2),
(1,'2003-01-01','2003-12-31',2),
(1,'2004-01-01','2004-12-31',2),
(1,'2005-01-01','2005-12-31',2),
(1,'2006-01-01','2006-12-31',2),

(1,'2014-01-01','2014-12-31',3),
(1,'2015-01-01','2015-12-31',3),
(1,'2017-06-30','2017-12-31',4),
(1,'2018-01-01',NULL,4),

(2,'2014-02-01','2015-01-31',1),
(2,'2015-02-01','2015-12-31',1),
(2,'2017-09-30','2018-01-31',2),
(2,'2018-02-01','2018-02-14',2)
;

CREATE OR REPLACE FUNCTION
        append_daterange(d1 daterange, d2 daterange)
        RETURNS daterange
        LANGUAGE sql
        AS
$$
        SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2 END;
$$;

CREATE AGGREGATE agg_daterange (daterange) (
        sfunc = append_daterange,
        stype = daterange
);

CREATE OR REPLACE FUNCTION range_continuous_merge( daterange, daterange,
date ) RETURNS daterange AS $$

DECLARE
    res daterange;

BEGIN
res:=  CASE
    WHEN $1 IS NULL AND NOT $2 @> $3 THEN NULL
    WHEN $1 IS NULL AND $2 @> $3 THEN $2
    WHEN ($1 && $2) OR ($1 -|- $2) THEN $1 + $2
    WHEN NOT $1 @> $3 THEN $2
    ELSE $1
END;
RAISE NOTICE 'Inputs: %,%,%.  Returning %',$1::text,$2::text,$3::text,res;
RETURN res;
END;
$$ LANGUAGE plpgsql STABLE;

CREATE AGGREGATE range_continuous( daterange, date ) (

    sfunc =  range_continuous_merge,
    stype = daterange
--  initcond = '{0,0,0}'

);

SELECT
client_id,episode,start_date,end_date,range_continuous(daterange(start_date,end_date,'[]'),start_date)
OVER (PARTITION by client_id) FROM sample_data ;

SELECT
client_id,episode,start_date,end_date,agg_daterange(daterange(start_date,end_date,'[]'))
OVER (PARTITION by client_id) FROM sample_data ;



-- RANGE_CONTINUOUS_MERGE

NOTICE:  Inputs: <NULL>,[1990-01-01,1991-01-01),1990-01-01.  Returning
[1990-01-01,1991-01-01)
NOTICE:  Inputs:
[1990-01-01,1991-01-01),[1991-01-01,1992-01-01),1991-01-01.  Returning
[1990-01-01,1992-01-01)
NOTICE:  Inputs:
[1990-01-01,1992-01-01),[2000-01-01,2001-01-01),2000-01-01.  Returning
[2000-01-01,2001-01-01)
NOTICE:  Inputs:
[2000-01-01,2001-01-01),[2001-01-01,2002-01-01),2001-01-01.  Returning
[2000-01-01,2002-01-01)
NOTICE:  Inputs:
[2000-01-01,2002-01-01),[2002-01-01,2003-01-01),2002-01-01.  Returning
[2000-01-01,2003-01-01)
NOTICE:  Inputs:
[2000-01-01,2003-01-01),[2003-01-01,2004-01-01),2003-01-01.  Returning
[2000-01-01,2004-01-01)
NOTICE:  Inputs:
[2000-01-01,2004-01-01),[2004-01-01,2005-01-01),2004-01-01.  Returning
[2000-01-01,2005-01-01)
NOTICE:  Inputs:
[2000-01-01,2005-01-01),[2005-01-01,2006-01-01),2005-01-01.  Returning
[2000-01-01,2006-01-01)
NOTICE:  Inputs:
[2000-01-01,2006-01-01),[2006-01-01,2007-01-01),2006-01-01.  Returning
[2000-01-01,2007-01-01)
NOTICE:  Inputs:
[2000-01-01,2007-01-01),[2014-01-01,2015-01-01),2014-01-01.  Returning
[2014-01-01,2015-01-01)
NOTICE:  Inputs:
[2014-01-01,2015-01-01),[2015-01-01,2016-01-01),2015-01-01.  Returning
[2014-01-01,2016-01-01)
NOTICE:  Inputs:
[2014-01-01,2016-01-01),[2017-06-30,2018-01-01),2017-06-30.  Returning
[2017-06-30,2018-01-01)
NOTICE:  Inputs: [2017-06-30,2018-01-01),[2018-01-01,),2018-01-01.
Returning [2017-06-30,)
NOTICE:  Inputs: <NULL>,[2014-02-01,2015-02-01),2014-02-01.  Returning
[2014-02-01,2015-02-01)
NOTICE:  Inputs:
[2014-02-01,2015-02-01),[2015-02-01,2016-01-01),2015-02-01.  Returning
[2014-02-01,2016-01-01)
NOTICE:  Inputs:
[2014-02-01,2016-01-01),[2017-09-30,2018-02-01),2017-09-30.  Returning
[2017-09-30,2018-02-01)
NOTICE:  Inputs:
[2017-09-30,2018-02-01),[2018-02-01,2018-02-15),2018-02-01.  Returning
[2017-09-30,2018-02-15)

 client_id | episode | start_date |  end_date  |    range_continuous
-----------+---------+------------+------------+-------------------------
         1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
         1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
         1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
         1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
         1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
         1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
         1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
         1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
         1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
         1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
         1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
         1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
         1 |       4 | 2018-01-01 |            | [2017-06-30,)
         2 |       1 | 2014-02-01 | 2015-01-31 | [2017-09-30,2018-02-15)
         2 |       1 | 2015-02-01 | 2015-12-31 | [2017-09-30,2018-02-15)
         2 |       2 | 2017-09-30 | 2018-01-31 | [2017-09-30,2018-02-15)
         2 |       2 | 2018-02-01 | 2018-02-14 | [2017-09-30,2018-02-15)

(17 rows)


-- AGG_DATERANGE

 client_id | episode | start_date |  end_date  |      agg_daterange
-----------+---------+------------+------------+-------------------------
         1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
         1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
         1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
         1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
         1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
         1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
         1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
         1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
         1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
         1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
         1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
         1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
         1 |       4 | 2018-01-01 |            | [2017-06-30,)
         2 |       1 | 2014-02-01 | 2015-01-31 | [2017-09-30,2018-02-15)
         2 |       1 | 2015-02-01 | 2015-12-31 | [2017-09-30,2018-02-15)
         2 |       2 | 2017-09-30 | 2018-01-31 | [2017-09-30,2018-02-15)
         2 |       2 | 2018-02-01 | 2018-02-14 | [2017-09-30,2018-02-15)
(17 rows)



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

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

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates)
@ 2018-02-27 09:03  mariusz <marius@mtvk.pl>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  0 siblings, 2 replies; 18+ messages in thread

From: mariusz @ 2018-02-27 09:03 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general

On Fri, 2018-02-23 at 18:11 -0800, Ken Tanzer wrote:
>         On Fri, Feb 23, 2018 at 6:10 AM,
>         mariusz <marius@mtvk.pl> wrote: 
>         
>         
>         i guess, you can easily get max continuous range for each row
>         with
>         something like this:
>         
>         CREATE OR REPLACE FUNCTION
>                 append_daterange(d1 daterange, d2 daterange)
>                 RETURNS daterange
>                 LANGUAGE sql
>                 AS
>         $$
>                 SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2
>         ELSE d2 END;
>         $$;
>         
>         CREATE AGGREGATE agg_daterange (daterange) (
>                 sfunc = append_daterange,
>                 stype = daterange
>         );
>         
>         SELECT dr,
>                lower(agg_daterange(dr) OVER (ORDER BY dr ASC)),
>                upper(agg_daterange(dr) OVER (ORDER BY dr DESC))
>           FROM ...
>         
>         above example is simplified to selecting only daterange column
>         "dr" for
>         readability, which in your case should be something like
>         
>         daterange(staff_assign_date,staff_assign_date_end,'[)')
>         
>         please note that daterange would be normalized to [) format so
>         upper()
>         above returns exactly your max "staff_assign_date_end" for
>         each
>         continuous range when dateranges are created with '[)' format.
>         
>         the key point is ... ELSE d2 in append_daterange() which
>         starts with new
>         value each time that new value is discontinuous with agg's
>         state value
>         and order in which rows are processed (ASC for lower of
>         daterange, DESC
>         for upper of daterange).
>         
>         unfortunately this involves reading all rows for "client_id"
>         and
>         additional sorting for each window.
>         i recall reading that you already pay the price of reading all
>         rows for
>         client_id anyway, so the only question is the cost of two
>         additional
>         sorts (maybe extracting dateranges to subset on which to do
>         windows and
>         rejoining result of continuous ranges to original set would
>         help to
>         lower the cost).
>         
> 
> Thank you, and I wanted to follow up on this.  I couldn't quite get
> your example working as described, but I also ended up trying
> something very similar that got me very close but not quite there.
> Basically, I can see that it is correctly calculating the ranges (in
> the notices), but it is only returning the last range for each client.
> (Because I said PARTITION BY client_id).
> 
> 
sorry for late replay, i was offline from sat to mon inclusive.
i may have previously added some confusion, so i'll try to explain what
i had in my mind. see below what my suggested query is.


> So I'm not sure if I should be calling this differently, or if the
> function needs to work differently, or if this just isn't possible.
> Do I need to partition by something else, and if so what?  I don't see
> what I could specify that would indicate a new episode.
> 
definitely you want to partition by client_id if you are calculating
this for multiple client_ids, but what matters here for each client_id
is order of dateranges in over()

> 
> Also, it's not clear to me how an aggregate might define/return
> different values within a partition.  Although this must be possible,
> since functions like rank() and row_number() seem to do it.
> 
that is because function is defined like agg, but what happens is that
its current state value (return value of agg function) is returned for
every row within given window without grouping the resultset
> 
> Hoping there is something easy that can be tweaked here.  See below
> for copy/pastable test stuff.  It includes output from both functions.
> Both look to be returning the same results, which makes me wonder if
> my passing in a start date was a waste of time, though it seems to me
> it would be necessary.
> 
> Cheers,
> Ken
> 
> 
> BEGIN;
> CREATE TEMP TABLE sample_data (
> client_id INTEGER,
> start_date DATE,
> end_date DATE,
> episode INTEGER -- Just a label, for clarity
> );
> 
> INSERT INTO sample_data VALUES
> (1,'1990-01-01','1990-12-31',1),
> (1,'1991-01-01','1991-12-31',1),
> 
> (1,'2000-01-01','2000-12-31',2),
> (1,'2001-01-01','2001-12-31',2),
> (1,'2002-01-01','2002-12-31',2),
> (1,'2003-01-01','2003-12-31',2),
> (1,'2004-01-01','2004-12-31',2),
> (1,'2005-01-01','2005-12-31',2),
> (1,'2006-01-01','2006-12-31',2),
> 
> (1,'2014-01-01','2014-12-31',3),
> (1,'2015-01-01','2015-12-31',3),
> (1,'2017-06-30','2017-12-31',4),
> (1,'2018-01-01',NULL,4),
> 
> (2,'2014-02-01','2015-01-31',1),
> (2,'2015-02-01','2015-12-31',1),
> (2,'2017-09-30','2018-01-31',2),
> (2,'2018-02-01','2018-02-14',2)
> ;
> 
> CREATE OR REPLACE FUNCTION
>         append_daterange(d1 daterange, d2 daterange)
>         RETURNS daterange
>         LANGUAGE sql
>         AS
> $$
>         SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2
> END;
> $$;
> 
> CREATE AGGREGATE agg_daterange (daterange) (
>         sfunc = append_daterange,
>         stype = daterange
> );
> 


based on your sample data see the query:

select client_id, d,
       daterange(lower(test_agg_daterange(d) 
                          over (partition by client_id
                                order by d)),               
                 upper(test_agg_daterange(d)
                         over (partition by client_id
                               order by d desc)),
                 '[)'
                ) as cont_range
  from (select *, daterange(start_date,end_date,'[]') as d
        from sample_data
       ) q
order by 1, 2;

 client_id |            d            |       cont_range        
-----------+-------------------------+-------------------------
         1 | [1990-01-01,1991-01-01) | [1990-01-01,1992-01-01)
         1 | [1991-01-01,1992-01-01) | [1990-01-01,1992-01-01)
         1 | [2000-01-01,2001-01-01) | [2000-01-01,2007-01-01)
         1 | [2001-01-01,2002-01-01) | [2000-01-01,2007-01-01)
         1 | [2002-01-01,2003-01-01) | [2000-01-01,2007-01-01)
         1 | [2003-01-01,2004-01-01) | [2000-01-01,2007-01-01)
         1 | [2004-01-01,2005-01-01) | [2000-01-01,2007-01-01)
         1 | [2005-01-01,2006-01-01) | [2000-01-01,2007-01-01)
         1 | [2006-01-01,2007-01-01) | [2000-01-01,2007-01-01)
         1 | [2014-01-01,2015-01-01) | [2014-01-01,2016-01-01)
         1 | [2015-01-01,2016-01-01) | [2014-01-01,2016-01-01)
         1 | [2017-06-30,2018-01-01) | [2017-06-30,)
         1 | [2018-01-01,)           | [2017-06-30,)
         2 | [2014-02-01,2015-02-01) | [2014-02-01,2016-01-01)
         2 | [2015-02-01,2016-01-01) | [2014-02-01,2016-01-01)
         2 | [2017-09-30,2018-02-01) | [2017-09-30,2018-02-15)
         2 | [2018-02-01,2018-02-15) | [2017-09-30,2018-02-15)
(17 rows)
  
and time to explain confusion i added previously regarding daterange
input format (i'm working with something similar, but with dateranges in
db, not add hoc generated from start and end dates).
you need to generate daterange(start_date,end_date,'[]') for -|-
operator to work correctly, but output of daterange will be normalized
to '[)' format so it's up to you to extract end_date of continuous range
(if you prefer dates over dateranges) which gonna be something like
(upper(cont_range)-'1 day'::interval)::date

above example is for showing how it works, returns max continuous range
containing given row,
you probably want:
select distinct on (client_id,cont_range)
or something like that to extract continuous ranges

first window (within lower() func) extends its initial (earliest) range
to right for every next row continuous with current range (and jumps to
new start on discontinuity), thus over(order by d ASC)
second window (within upper() func) extends its initial (latest) range
to left for every next row continuous with current range (and jumps to
new end on discontinuity), thus over(order by d DESC)
partition by client_id within each over() is to not mix client_ids'
dateranges


i guess that is more or less what you wanted, at least as i understood
you originally, if not than sorry for even more confusion or
unnecessarily explaining simple and obvious things you might already
know

regards,
mariusz jadczak


> CREATE OR REPLACE FUNCTION range_continuous_merge( daterange,
> daterange, date ) RETURNS daterange AS $$
> 
> DECLARE
>     res daterange;
> 
> BEGIN
> res:=  CASE
>     WHEN $1 IS NULL AND NOT $2 @> $3 THEN NULL
>     WHEN $1 IS NULL AND $2 @> $3 THEN $2
>     WHEN ($1 && $2) OR ($1 -|- $2) THEN $1 + $2
>     WHEN NOT $1 @> $3 THEN $2
>     ELSE $1
> END;
> RAISE NOTICE 'Inputs: %,%,%.  Returning %',$1::text,$2::text,
> $3::text,res;
> RETURN res;
> END;
> $$ LANGUAGE plpgsql STABLE;
> 
> CREATE AGGREGATE range_continuous( daterange, date ) (
> 
>     sfunc =  range_continuous_merge,
>     stype = daterange
> --  initcond = '{0,0,0}'
> 
> );
> 
> SELECT
> client_id,episode,start_date,end_date,range_continuous(daterange(start_date,end_date,'[]'),start_date) OVER (PARTITION by client_id) FROM sample_data ;
> 
> SELECT
> client_id,episode,start_date,end_date,agg_daterange(daterange(start_date,end_date,'[]')) OVER (PARTITION by client_id) FROM sample_data ;
> 
> 
> 
> -- RANGE_CONTINUOUS_MERGE
> 
> NOTICE:  Inputs: <NULL>,[1990-01-01,1991-01-01),1990-01-01.  Returning
> [1990-01-01,1991-01-01)
> NOTICE:  Inputs:
> [1990-01-01,1991-01-01),[1991-01-01,1992-01-01),1991-01-01.  Returning
> [1990-01-01,1992-01-01)
> NOTICE:  Inputs:
> [1990-01-01,1992-01-01),[2000-01-01,2001-01-01),2000-01-01.  Returning
> [2000-01-01,2001-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2001-01-01),[2001-01-01,2002-01-01),2001-01-01.  Returning
> [2000-01-01,2002-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2002-01-01),[2002-01-01,2003-01-01),2002-01-01.  Returning
> [2000-01-01,2003-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2003-01-01),[2003-01-01,2004-01-01),2003-01-01.  Returning
> [2000-01-01,2004-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2004-01-01),[2004-01-01,2005-01-01),2004-01-01.  Returning
> [2000-01-01,2005-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2005-01-01),[2005-01-01,2006-01-01),2005-01-01.  Returning
> [2000-01-01,2006-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2006-01-01),[2006-01-01,2007-01-01),2006-01-01.  Returning
> [2000-01-01,2007-01-01)
> NOTICE:  Inputs:
> [2000-01-01,2007-01-01),[2014-01-01,2015-01-01),2014-01-01.  Returning
> [2014-01-01,2015-01-01)
> NOTICE:  Inputs:
> [2014-01-01,2015-01-01),[2015-01-01,2016-01-01),2015-01-01.  Returning
> [2014-01-01,2016-01-01)
> NOTICE:  Inputs:
> [2014-01-01,2016-01-01),[2017-06-30,2018-01-01),2017-06-30.  Returning
> [2017-06-30,2018-01-01)
> NOTICE:  Inputs: [2017-06-30,2018-01-01),[2018-01-01,),2018-01-01.
> Returning [2017-06-30,)
> NOTICE:  Inputs: <NULL>,[2014-02-01,2015-02-01),2014-02-01.  Returning
> [2014-02-01,2015-02-01)
> NOTICE:  Inputs:
> [2014-02-01,2015-02-01),[2015-02-01,2016-01-01),2015-02-01.  Returning
> [2014-02-01,2016-01-01)
> NOTICE:  Inputs:
> [2014-02-01,2016-01-01),[2017-09-30,2018-02-01),2017-09-30.  Returning
> [2017-09-30,2018-02-01)
> NOTICE:  Inputs:
> [2017-09-30,2018-02-01),[2018-02-01,2018-02-15),2018-02-01.  Returning
> [2017-09-30,2018-02-15)
> 
>  client_id | episode | start_date |  end_date  |    range_continuous
> -----------+---------+------------+------------+-------------------------
>          1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
>          1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
>          1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
>          1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
>          1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
>          1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
>          1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
>          1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
>          1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
>          1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
>          1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
>          1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
>          1 |       4 | 2018-01-01 |            | [2017-06-30,)
>          2 |       1 | 2014-02-01 | 2015-01-31 |
> [2017-09-30,2018-02-15)
>          2 |       1 | 2015-02-01 | 2015-12-31 |
> [2017-09-30,2018-02-15)
>          2 |       2 | 2017-09-30 | 2018-01-31 |
> [2017-09-30,2018-02-15)
>          2 |       2 | 2018-02-01 | 2018-02-14 |
> [2017-09-30,2018-02-15)
> 
> (17 rows)
> 
> 
> -- AGG_DATERANGE
> 
>  client_id | episode | start_date |  end_date  |      agg_daterange
>    
> -----------+---------+------------+------------+-------------------------
>          1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
>          1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
>          1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
>          1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
>          1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
>          1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
>          1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
>          1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
>          1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
>          1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
>          1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
>          1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
>          1 |       4 | 2018-01-01 |            | [2017-06-30,)
>          2 |       1 | 2014-02-01 | 2015-01-31 |
> [2017-09-30,2018-02-15)
>          2 |       1 | 2015-02-01 | 2015-12-31 |
> [2017-09-30,2018-02-15)
>          2 |       2 | 2017-09-30 | 2018-01-31 |
> [2017-09-30,2018-02-15)
>          2 |       2 | 2018-02-01 | 2018-02-14 |
> [2017-09-30,2018-02-15)
> (17 rows)
> 
> 
> 
> 
> 
> 
> -- 
> 
> 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
> (253) 245-3801
> 
> 
> Subscribe to the mailing list to
> learn more about AGENCY or
> follow the discussion.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates)
@ 2018-02-27 09:16  mariusz <marius@mtvk.pl>
  parent: mariusz <marius@mtvk.pl>
  1 sibling, 0 replies; 18+ messages in thread

From: mariusz @ 2018-02-27 09:16 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general



On Tue, 2018-02-27 at 10:03 +0100, mariusz wrote:
> On Fri, 2018-02-23 at 18:11 -0800, Ken Tanzer wrote:
> >         On Fri, Feb 23, 2018 at 6:10 AM,
> >         mariusz <marius@mtvk.pl> wrote: 
> >         
> >         
> >         i guess, you can easily get max continuous range for each row
> >         with
> >         something like this:
> >         
> >         CREATE OR REPLACE FUNCTION
> >                 append_daterange(d1 daterange, d2 daterange)
> >                 RETURNS daterange
> >                 LANGUAGE sql
> >                 AS
> >         $$
> >                 SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2
> >         ELSE d2 END;
> >         $$;
> >         
> >         CREATE AGGREGATE agg_daterange (daterange) (
> >                 sfunc = append_daterange,
> >                 stype = daterange
> >         );
> >         
> >         SELECT dr,
> >                lower(agg_daterange(dr) OVER (ORDER BY dr ASC)),
> >                upper(agg_daterange(dr) OVER (ORDER BY dr DESC))
> >           FROM ...
> >         
> >         above example is simplified to selecting only daterange column
> >         "dr" for
> >         readability, which in your case should be something like
> >         
> >         daterange(staff_assign_date,staff_assign_date_end,'[)')
> >         
> >         please note that daterange would be normalized to [) format so
> >         upper()
> >         above returns exactly your max "staff_assign_date_end" for
> >         each
> >         continuous range when dateranges are created with '[)' format.
> >         
> >         the key point is ... ELSE d2 in append_daterange() which
> >         starts with new
> >         value each time that new value is discontinuous with agg's
> >         state value
> >         and order in which rows are processed (ASC for lower of
> >         daterange, DESC
> >         for upper of daterange).
> >         
> >         unfortunately this involves reading all rows for "client_id"
> >         and
> >         additional sorting for each window.
> >         i recall reading that you already pay the price of reading all
> >         rows for
> >         client_id anyway, so the only question is the cost of two
> >         additional
> >         sorts (maybe extracting dateranges to subset on which to do
> >         windows and
> >         rejoining result of continuous ranges to original set would
> >         help to
> >         lower the cost).
> >         
> > 
> > Thank you, and I wanted to follow up on this.  I couldn't quite get
> > your example working as described, but I also ended up trying
> > something very similar that got me very close but not quite there.
> > Basically, I can see that it is correctly calculating the ranges (in
> > the notices), but it is only returning the last range for each client.
> > (Because I said PARTITION BY client_id).
> > 
> > 
> sorry for late replay, i was offline from sat to mon inclusive.
> i may have previously added some confusion, so i'll try to explain what
> i had in my mind. see below what my suggested query is.
> 
> 
> > So I'm not sure if I should be calling this differently, or if the
> > function needs to work differently, or if this just isn't possible.
> > Do I need to partition by something else, and if so what?  I don't see
> > what I could specify that would indicate a new episode.
> > 
> definitely you want to partition by client_id if you are calculating
> this for multiple client_ids, but what matters here for each client_id
> is order of dateranges in over()
> 
> > 
> > Also, it's not clear to me how an aggregate might define/return
> > different values within a partition.  Although this must be possible,
> > since functions like rank() and row_number() seem to do it.
> > 
> that is because function is defined like agg, but what happens is that
> its current state value (return value of agg function) is returned for
> every row within given window without grouping the resultset
> > 
> > Hoping there is something easy that can be tweaked here.  See below
> > for copy/pastable test stuff.  It includes output from both functions.
> > Both look to be returning the same results, which makes me wonder if
> > my passing in a start date was a waste of time, though it seems to me
> > it would be necessary.
> > 
> > Cheers,
> > Ken
> > 
> > 
> > BEGIN;
> > CREATE TEMP TABLE sample_data (
> > client_id INTEGER,
> > start_date DATE,
> > end_date DATE,
> > episode INTEGER -- Just a label, for clarity
> > );
> > 
> > INSERT INTO sample_data VALUES
> > (1,'1990-01-01','1990-12-31',1),
> > (1,'1991-01-01','1991-12-31',1),
> > 
> > (1,'2000-01-01','2000-12-31',2),
> > (1,'2001-01-01','2001-12-31',2),
> > (1,'2002-01-01','2002-12-31',2),
> > (1,'2003-01-01','2003-12-31',2),
> > (1,'2004-01-01','2004-12-31',2),
> > (1,'2005-01-01','2005-12-31',2),
> > (1,'2006-01-01','2006-12-31',2),
> > 
> > (1,'2014-01-01','2014-12-31',3),
> > (1,'2015-01-01','2015-12-31',3),
> > (1,'2017-06-30','2017-12-31',4),
> > (1,'2018-01-01',NULL,4),
> > 
> > (2,'2014-02-01','2015-01-31',1),
> > (2,'2015-02-01','2015-12-31',1),
> > (2,'2017-09-30','2018-01-31',2),
> > (2,'2018-02-01','2018-02-14',2)
> > ;
> > 
> > CREATE OR REPLACE FUNCTION
> >         append_daterange(d1 daterange, d2 daterange)
> >         RETURNS daterange
> >         LANGUAGE sql
> >         AS
> > $$
> >         SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2
> > END;
> > $$;
> > 
> > CREATE AGGREGATE agg_daterange (daterange) (
> >         sfunc = append_daterange,
> >         stype = daterange
> > );
> > 
> 
> 
> based on your sample data see the query:
> 
> select client_id, d,
>        daterange(lower(test_agg_daterange(d) 
>                           over (partition by client_id
>                                 order by d)),               
>                  upper(test_agg_daterange(d)
>                          over (partition by client_id
>                                order by d desc)),
>                  '[)'
>                 ) as cont_range
>   from (select *, daterange(start_date,end_date,'[]') as d
>         from sample_data
>        ) q
> order by 1, 2;
> 

that test_agg_daterange() is of course exactly the same as
agg_daterange() agg above, sorry for not cleaning enough my examples
lazily edited and copied from my history in psql

regards, mj


>  client_id |            d            |       cont_range        
> -----------+-------------------------+-------------------------
>          1 | [1990-01-01,1991-01-01) | [1990-01-01,1992-01-01)
>          1 | [1991-01-01,1992-01-01) | [1990-01-01,1992-01-01)
>          1 | [2000-01-01,2001-01-01) | [2000-01-01,2007-01-01)
>          1 | [2001-01-01,2002-01-01) | [2000-01-01,2007-01-01)
>          1 | [2002-01-01,2003-01-01) | [2000-01-01,2007-01-01)
>          1 | [2003-01-01,2004-01-01) | [2000-01-01,2007-01-01)
>          1 | [2004-01-01,2005-01-01) | [2000-01-01,2007-01-01)
>          1 | [2005-01-01,2006-01-01) | [2000-01-01,2007-01-01)
>          1 | [2006-01-01,2007-01-01) | [2000-01-01,2007-01-01)
>          1 | [2014-01-01,2015-01-01) | [2014-01-01,2016-01-01)
>          1 | [2015-01-01,2016-01-01) | [2014-01-01,2016-01-01)
>          1 | [2017-06-30,2018-01-01) | [2017-06-30,)
>          1 | [2018-01-01,)           | [2017-06-30,)
>          2 | [2014-02-01,2015-02-01) | [2014-02-01,2016-01-01)
>          2 | [2015-02-01,2016-01-01) | [2014-02-01,2016-01-01)
>          2 | [2017-09-30,2018-02-01) | [2017-09-30,2018-02-15)
>          2 | [2018-02-01,2018-02-15) | [2017-09-30,2018-02-15)
> (17 rows)
>   
> and time to explain confusion i added previously regarding daterange
> input format (i'm working with something similar, but with dateranges in
> db, not add hoc generated from start and end dates).
> you need to generate daterange(start_date,end_date,'[]') for -|-
> operator to work correctly, but output of daterange will be normalized
> to '[)' format so it's up to you to extract end_date of continuous range
> (if you prefer dates over dateranges) which gonna be something like
> (upper(cont_range)-'1 day'::interval)::date
> 
> above example is for showing how it works, returns max continuous range
> containing given row,
> you probably want:
> select distinct on (client_id,cont_range)
> or something like that to extract continuous ranges
> 
> first window (within lower() func) extends its initial (earliest) range
> to right for every next row continuous with current range (and jumps to
> new start on discontinuity), thus over(order by d ASC)
> second window (within upper() func) extends its initial (latest) range
> to left for every next row continuous with current range (and jumps to
> new end on discontinuity), thus over(order by d DESC)
> partition by client_id within each over() is to not mix client_ids'
> dateranges
> 
> 
> i guess that is more or less what you wanted, at least as i understood
> you originally, if not than sorry for even more confusion or
> unnecessarily explaining simple and obvious things you might already
> know
> 
> regards,
> mariusz jadczak
> 
> 
> > CREATE OR REPLACE FUNCTION range_continuous_merge( daterange,
> > daterange, date ) RETURNS daterange AS $$
> > 
> > DECLARE
> >     res daterange;
> > 
> > BEGIN
> > res:=  CASE
> >     WHEN $1 IS NULL AND NOT $2 @> $3 THEN NULL
> >     WHEN $1 IS NULL AND $2 @> $3 THEN $2
> >     WHEN ($1 && $2) OR ($1 -|- $2) THEN $1 + $2
> >     WHEN NOT $1 @> $3 THEN $2
> >     ELSE $1
> > END;
> > RAISE NOTICE 'Inputs: %,%,%.  Returning %',$1::text,$2::text,
> > $3::text,res;
> > RETURN res;
> > END;
> > $$ LANGUAGE plpgsql STABLE;
> > 
> > CREATE AGGREGATE range_continuous( daterange, date ) (
> > 
> >     sfunc =  range_continuous_merge,
> >     stype = daterange
> > --  initcond = '{0,0,0}'
> > 
> > );
> > 
> > SELECT
> > client_id,episode,start_date,end_date,range_continuous(daterange(start_date,end_date,'[]'),start_date) OVER (PARTITION by client_id) FROM sample_data ;
> > 
> > SELECT
> > client_id,episode,start_date,end_date,agg_daterange(daterange(start_date,end_date,'[]')) OVER (PARTITION by client_id) FROM sample_data ;
> > 
> > 
> > 
> > -- RANGE_CONTINUOUS_MERGE
> > 
> > NOTICE:  Inputs: <NULL>,[1990-01-01,1991-01-01),1990-01-01.  Returning
> > [1990-01-01,1991-01-01)
> > NOTICE:  Inputs:
> > [1990-01-01,1991-01-01),[1991-01-01,1992-01-01),1991-01-01.  Returning
> > [1990-01-01,1992-01-01)
> > NOTICE:  Inputs:
> > [1990-01-01,1992-01-01),[2000-01-01,2001-01-01),2000-01-01.  Returning
> > [2000-01-01,2001-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2001-01-01),[2001-01-01,2002-01-01),2001-01-01.  Returning
> > [2000-01-01,2002-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2002-01-01),[2002-01-01,2003-01-01),2002-01-01.  Returning
> > [2000-01-01,2003-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2003-01-01),[2003-01-01,2004-01-01),2003-01-01.  Returning
> > [2000-01-01,2004-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2004-01-01),[2004-01-01,2005-01-01),2004-01-01.  Returning
> > [2000-01-01,2005-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2005-01-01),[2005-01-01,2006-01-01),2005-01-01.  Returning
> > [2000-01-01,2006-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2006-01-01),[2006-01-01,2007-01-01),2006-01-01.  Returning
> > [2000-01-01,2007-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2007-01-01),[2014-01-01,2015-01-01),2014-01-01.  Returning
> > [2014-01-01,2015-01-01)
> > NOTICE:  Inputs:
> > [2014-01-01,2015-01-01),[2015-01-01,2016-01-01),2015-01-01.  Returning
> > [2014-01-01,2016-01-01)
> > NOTICE:  Inputs:
> > [2014-01-01,2016-01-01),[2017-06-30,2018-01-01),2017-06-30.  Returning
> > [2017-06-30,2018-01-01)
> > NOTICE:  Inputs: [2017-06-30,2018-01-01),[2018-01-01,),2018-01-01.
> > Returning [2017-06-30,)
> > NOTICE:  Inputs: <NULL>,[2014-02-01,2015-02-01),2014-02-01.  Returning
> > [2014-02-01,2015-02-01)
> > NOTICE:  Inputs:
> > [2014-02-01,2015-02-01),[2015-02-01,2016-01-01),2015-02-01.  Returning
> > [2014-02-01,2016-01-01)
> > NOTICE:  Inputs:
> > [2014-02-01,2016-01-01),[2017-09-30,2018-02-01),2017-09-30.  Returning
> > [2017-09-30,2018-02-01)
> > NOTICE:  Inputs:
> > [2017-09-30,2018-02-01),[2018-02-01,2018-02-15),2018-02-01.  Returning
> > [2017-09-30,2018-02-15)
> > 
> >  client_id | episode | start_date |  end_date  |    range_continuous
> > -----------+---------+------------+------------+-------------------------
> >          1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
> >          1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
> >          1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
> >          1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
> >          1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
> >          1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
> >          1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
> >          1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
> >          1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
> >          1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
> >          1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
> >          1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
> >          1 |       4 | 2018-01-01 |            | [2017-06-30,)
> >          2 |       1 | 2014-02-01 | 2015-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       1 | 2015-02-01 | 2015-12-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2017-09-30 | 2018-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2018-02-01 | 2018-02-14 |
> > [2017-09-30,2018-02-15)
> > 
> > (17 rows)
> > 
> > 
> > -- AGG_DATERANGE
> > 
> >  client_id | episode | start_date |  end_date  |      agg_daterange
> >    
> > -----------+---------+------------+------------+-------------------------
> >          1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
> >          1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
> >          1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
> >          1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
> >          1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
> >          1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
> >          1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
> >          1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
> >          1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
> >          1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
> >          1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
> >          1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
> >          1 |       4 | 2018-01-01 |            | [2017-06-30,)
> >          2 |       1 | 2014-02-01 | 2015-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       1 | 2015-02-01 | 2015-12-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2017-09-30 | 2018-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2018-02-01 | 2018-02-14 |
> > [2017-09-30,2018-02-15)
> > (17 rows)
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > 
> > 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
> > (253) 245-3801
> > 
> > 
> > Subscribe to the mailing list to
> > learn more about AGENCY or
> > follow the discussion.
> 
> 
> 






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates)
@ 2018-02-27 11:26  mariusz <marius@mtvk.pl>
  parent: mariusz <marius@mtvk.pl>
  1 sibling, 1 reply; 18+ messages in thread

From: mariusz @ 2018-02-27 11:26 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general


hello,

one more fix, to not let someone get incorrect/incomplete ideas, see
below


On Tue, 2018-02-27 at 10:03 +0100, mariusz wrote:
> On Fri, 2018-02-23 at 18:11 -0800, Ken Tanzer wrote:
> >         On Fri, Feb 23, 2018 at 6:10 AM,
> >         mariusz <marius@mtvk.pl> wrote: 
> >         
> >         
> >         i guess, you can easily get max continuous range for each row
> >         with
> >         something like this:
> >         
> >         CREATE OR REPLACE FUNCTION
> >                 append_daterange(d1 daterange, d2 daterange)
> >                 RETURNS daterange
> >                 LANGUAGE sql
> >                 AS
> >         $$
> >                 SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2
> >         ELSE d2 END;
> >         $$;
> >         
> >         CREATE AGGREGATE agg_daterange (daterange) (
> >                 sfunc = append_daterange,
> >                 stype = daterange
> >         );
> >         
> >         SELECT dr,
> >                lower(agg_daterange(dr) OVER (ORDER BY dr ASC)),
> >                upper(agg_daterange(dr) OVER (ORDER BY dr DESC))
> >           FROM ...
> >         
> >         above example is simplified to selecting only daterange column
> >         "dr" for
> >         readability, which in your case should be something like
> >         
> >         daterange(staff_assign_date,staff_assign_date_end,'[)')
> >         
> >         please note that daterange would be normalized to [) format so
> >         upper()
> >         above returns exactly your max "staff_assign_date_end" for
> >         each
> >         continuous range when dateranges are created with '[)' format.
> >         
> >         the key point is ... ELSE d2 in append_daterange() which
> >         starts with new
> >         value each time that new value is discontinuous with agg's
> >         state value
> >         and order in which rows are processed (ASC for lower of
> >         daterange, DESC
> >         for upper of daterange).
> >         
> >         unfortunately this involves reading all rows for "client_id"
> >         and
> >         additional sorting for each window.
> >         i recall reading that you already pay the price of reading all
> >         rows for
> >         client_id anyway, so the only question is the cost of two
> >         additional
> >         sorts (maybe extracting dateranges to subset on which to do
> >         windows and
> >         rejoining result of continuous ranges to original set would
> >         help to
> >         lower the cost).
> >         
> > 
> > Thank you, and I wanted to follow up on this.  I couldn't quite get
> > your example working as described, but I also ended up trying
> > something very similar that got me very close but not quite there.
> > Basically, I can see that it is correctly calculating the ranges (in
> > the notices), but it is only returning the last range for each client.
> > (Because I said PARTITION BY client_id).
> > 
> > 
> sorry for late replay, i was offline from sat to mon inclusive.
> i may have previously added some confusion, so i'll try to explain what
> i had in my mind. see below what my suggested query is.
> 
> 
> > So I'm not sure if I should be calling this differently, or if the
> > function needs to work differently, or if this just isn't possible.
> > Do I need to partition by something else, and if so what?  I don't see
> > what I could specify that would indicate a new episode.
> > 
> definitely you want to partition by client_id if you are calculating
> this for multiple client_ids, but what matters here for each client_id
> is order of dateranges in over()
> 
> > 
> > Also, it's not clear to me how an aggregate might define/return
> > different values within a partition.  Although this must be possible,
> > since functions like rank() and row_number() seem to do it.
> > 
> that is because function is defined like agg, but what happens is that
> its current state value (return value of agg function) is returned for
> every row within given window without grouping the resultset
> > 
> > Hoping there is something easy that can be tweaked here.  See below
> > for copy/pastable test stuff.  It includes output from both functions.
> > Both look to be returning the same results, which makes me wonder if
> > my passing in a start date was a waste of time, though it seems to me
> > it would be necessary.
> > 
> > Cheers,
> > Ken
> > 
> > 
> > BEGIN;
> > CREATE TEMP TABLE sample_data (
> > client_id INTEGER,
> > start_date DATE,
> > end_date DATE,
> > episode INTEGER -- Just a label, for clarity
> > );
> > 
> > INSERT INTO sample_data VALUES
> > (1,'1990-01-01','1990-12-31',1),
> > (1,'1991-01-01','1991-12-31',1),
> > 
> > (1,'2000-01-01','2000-12-31',2),
> > (1,'2001-01-01','2001-12-31',2),
> > (1,'2002-01-01','2002-12-31',2),
> > (1,'2003-01-01','2003-12-31',2),
> > (1,'2004-01-01','2004-12-31',2),
> > (1,'2005-01-01','2005-12-31',2),
> > (1,'2006-01-01','2006-12-31',2),
> > 
> > (1,'2014-01-01','2014-12-31',3),
> > (1,'2015-01-01','2015-12-31',3),
> > (1,'2017-06-30','2017-12-31',4),
> > (1,'2018-01-01',NULL,4),
> > 
> > (2,'2014-02-01','2015-01-31',1),
> > (2,'2015-02-01','2015-12-31',1),
> > (2,'2017-09-30','2018-01-31',2),
> > (2,'2018-02-01','2018-02-14',2)
> > ;
> > 
> > CREATE OR REPLACE FUNCTION
> >         append_daterange(d1 daterange, d2 daterange)
> >         RETURNS daterange
> >         LANGUAGE sql
> >         AS
> > $$
> >         SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2
> > END;
> > $$;
> > 
> > CREATE AGGREGATE agg_daterange (daterange) (
> >         sfunc = append_daterange,
> >         stype = daterange
> > );
> > 
> 
> 
> based on your sample data see the query:
> 
> select client_id, d,
>        daterange(lower(test_agg_daterange(d) 
>                           over (partition by client_id
>                                 order by d)),               
>                  upper(test_agg_daterange(d)
>                          over (partition by client_id
>                                order by d desc)),
>                  '[)'
>                 ) as cont_range
>   from (select *, daterange(start_date,end_date,'[]') as d
>         from sample_data
>        ) q
> order by 1, 2;


that should work with your non-overlapping ranges, should work with
overlapping ranges except for ranges strictly included within another.
that might not apply to your case, but for safety (we can't say who
gonna read this and get some ideas), i feel it's better to correct
second window, that within upper() to:

OVER (PARTITION BY client_id ORDER BY end_date DESC)

likewise, first window (within lower()) could be sorted by start_date
asc, but that's cosmetic change, while for upper bound of range it does
matter

regards, mj


>  client_id |            d            |       cont_range        
> -----------+-------------------------+-------------------------
>          1 | [1990-01-01,1991-01-01) | [1990-01-01,1992-01-01)
>          1 | [1991-01-01,1992-01-01) | [1990-01-01,1992-01-01)
>          1 | [2000-01-01,2001-01-01) | [2000-01-01,2007-01-01)
>          1 | [2001-01-01,2002-01-01) | [2000-01-01,2007-01-01)
>          1 | [2002-01-01,2003-01-01) | [2000-01-01,2007-01-01)
>          1 | [2003-01-01,2004-01-01) | [2000-01-01,2007-01-01)
>          1 | [2004-01-01,2005-01-01) | [2000-01-01,2007-01-01)
>          1 | [2005-01-01,2006-01-01) | [2000-01-01,2007-01-01)
>          1 | [2006-01-01,2007-01-01) | [2000-01-01,2007-01-01)
>          1 | [2014-01-01,2015-01-01) | [2014-01-01,2016-01-01)
>          1 | [2015-01-01,2016-01-01) | [2014-01-01,2016-01-01)
>          1 | [2017-06-30,2018-01-01) | [2017-06-30,)
>          1 | [2018-01-01,)           | [2017-06-30,)
>          2 | [2014-02-01,2015-02-01) | [2014-02-01,2016-01-01)
>          2 | [2015-02-01,2016-01-01) | [2014-02-01,2016-01-01)
>          2 | [2017-09-30,2018-02-01) | [2017-09-30,2018-02-15)
>          2 | [2018-02-01,2018-02-15) | [2017-09-30,2018-02-15)
> (17 rows)
>   
> and time to explain confusion i added previously regarding daterange
> input format (i'm working with something similar, but with dateranges in
> db, not add hoc generated from start and end dates).
> you need to generate daterange(start_date,end_date,'[]') for -|-
> operator to work correctly, but output of daterange will be normalized
> to '[)' format so it's up to you to extract end_date of continuous range
> (if you prefer dates over dateranges) which gonna be something like
> (upper(cont_range)-'1 day'::interval)::date
> 
> above example is for showing how it works, returns max continuous range
> containing given row,
> you probably want:
> select distinct on (client_id,cont_range)
> or something like that to extract continuous ranges
> 
> first window (within lower() func) extends its initial (earliest) range
> to right for every next row continuous with current range (and jumps to
> new start on discontinuity), thus over(order by d ASC)
> second window (within upper() func) extends its initial (latest) range
> to left for every next row continuous with current range (and jumps to
> new end on discontinuity), thus over(order by d DESC)
> partition by client_id within each over() is to not mix client_ids'
> dateranges
> 
> 
> i guess that is more or less what you wanted, at least as i understood
> you originally, if not than sorry for even more confusion or
> unnecessarily explaining simple and obvious things you might already
> know
> 
> regards,
> mariusz jadczak
> 
> 
> > CREATE OR REPLACE FUNCTION range_continuous_merge( daterange,
> > daterange, date ) RETURNS daterange AS $$
> > 
> > DECLARE
> >     res daterange;
> > 
> > BEGIN
> > res:=  CASE
> >     WHEN $1 IS NULL AND NOT $2 @> $3 THEN NULL
> >     WHEN $1 IS NULL AND $2 @> $3 THEN $2
> >     WHEN ($1 && $2) OR ($1 -|- $2) THEN $1 + $2
> >     WHEN NOT $1 @> $3 THEN $2
> >     ELSE $1
> > END;
> > RAISE NOTICE 'Inputs: %,%,%.  Returning %',$1::text,$2::text,
> > $3::text,res;
> > RETURN res;
> > END;
> > $$ LANGUAGE plpgsql STABLE;
> > 
> > CREATE AGGREGATE range_continuous( daterange, date ) (
> > 
> >     sfunc =  range_continuous_merge,
> >     stype = daterange
> > --  initcond = '{0,0,0}'
> > 
> > );
> > 
> > SELECT
> > client_id,episode,start_date,end_date,range_continuous(daterange(start_date,end_date,'[]'),start_date) OVER (PARTITION by client_id) FROM sample_data ;
> > 
> > SELECT
> > client_id,episode,start_date,end_date,agg_daterange(daterange(start_date,end_date,'[]')) OVER (PARTITION by client_id) FROM sample_data ;
> > 
> > 
> > 
> > -- RANGE_CONTINUOUS_MERGE
> > 
> > NOTICE:  Inputs: <NULL>,[1990-01-01,1991-01-01),1990-01-01.  Returning
> > [1990-01-01,1991-01-01)
> > NOTICE:  Inputs:
> > [1990-01-01,1991-01-01),[1991-01-01,1992-01-01),1991-01-01.  Returning
> > [1990-01-01,1992-01-01)
> > NOTICE:  Inputs:
> > [1990-01-01,1992-01-01),[2000-01-01,2001-01-01),2000-01-01.  Returning
> > [2000-01-01,2001-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2001-01-01),[2001-01-01,2002-01-01),2001-01-01.  Returning
> > [2000-01-01,2002-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2002-01-01),[2002-01-01,2003-01-01),2002-01-01.  Returning
> > [2000-01-01,2003-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2003-01-01),[2003-01-01,2004-01-01),2003-01-01.  Returning
> > [2000-01-01,2004-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2004-01-01),[2004-01-01,2005-01-01),2004-01-01.  Returning
> > [2000-01-01,2005-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2005-01-01),[2005-01-01,2006-01-01),2005-01-01.  Returning
> > [2000-01-01,2006-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2006-01-01),[2006-01-01,2007-01-01),2006-01-01.  Returning
> > [2000-01-01,2007-01-01)
> > NOTICE:  Inputs:
> > [2000-01-01,2007-01-01),[2014-01-01,2015-01-01),2014-01-01.  Returning
> > [2014-01-01,2015-01-01)
> > NOTICE:  Inputs:
> > [2014-01-01,2015-01-01),[2015-01-01,2016-01-01),2015-01-01.  Returning
> > [2014-01-01,2016-01-01)
> > NOTICE:  Inputs:
> > [2014-01-01,2016-01-01),[2017-06-30,2018-01-01),2017-06-30.  Returning
> > [2017-06-30,2018-01-01)
> > NOTICE:  Inputs: [2017-06-30,2018-01-01),[2018-01-01,),2018-01-01.
> > Returning [2017-06-30,)
> > NOTICE:  Inputs: <NULL>,[2014-02-01,2015-02-01),2014-02-01.  Returning
> > [2014-02-01,2015-02-01)
> > NOTICE:  Inputs:
> > [2014-02-01,2015-02-01),[2015-02-01,2016-01-01),2015-02-01.  Returning
> > [2014-02-01,2016-01-01)
> > NOTICE:  Inputs:
> > [2014-02-01,2016-01-01),[2017-09-30,2018-02-01),2017-09-30.  Returning
> > [2017-09-30,2018-02-01)
> > NOTICE:  Inputs:
> > [2017-09-30,2018-02-01),[2018-02-01,2018-02-15),2018-02-01.  Returning
> > [2017-09-30,2018-02-15)
> > 
> >  client_id | episode | start_date |  end_date  |    range_continuous
> > -----------+---------+------------+------------+-------------------------
> >          1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
> >          1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
> >          1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
> >          1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
> >          1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
> >          1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
> >          1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
> >          1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
> >          1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
> >          1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
> >          1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
> >          1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
> >          1 |       4 | 2018-01-01 |            | [2017-06-30,)
> >          2 |       1 | 2014-02-01 | 2015-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       1 | 2015-02-01 | 2015-12-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2017-09-30 | 2018-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2018-02-01 | 2018-02-14 |
> > [2017-09-30,2018-02-15)
> > 
> > (17 rows)
> > 
> > 
> > -- AGG_DATERANGE
> > 
> >  client_id | episode | start_date |  end_date  |      agg_daterange
> >    
> > -----------+---------+------------+------------+-------------------------
> >          1 |       1 | 1990-01-01 | 1990-12-31 | [2017-06-30,)
> >          1 |       1 | 1991-01-01 | 1991-12-31 | [2017-06-30,)
> >          1 |       2 | 2000-01-01 | 2000-12-31 | [2017-06-30,)
> >          1 |       2 | 2001-01-01 | 2001-12-31 | [2017-06-30,)
> >          1 |       2 | 2002-01-01 | 2002-12-31 | [2017-06-30,)
> >          1 |       2 | 2003-01-01 | 2003-12-31 | [2017-06-30,)
> >          1 |       2 | 2004-01-01 | 2004-12-31 | [2017-06-30,)
> >          1 |       2 | 2005-01-01 | 2005-12-31 | [2017-06-30,)
> >          1 |       2 | 2006-01-01 | 2006-12-31 | [2017-06-30,)
> >          1 |       3 | 2014-01-01 | 2014-12-31 | [2017-06-30,)
> >          1 |       3 | 2015-01-01 | 2015-12-31 | [2017-06-30,)
> >          1 |       4 | 2017-06-30 | 2017-12-31 | [2017-06-30,)
> >          1 |       4 | 2018-01-01 |            | [2017-06-30,)
> >          2 |       1 | 2014-02-01 | 2015-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       1 | 2015-02-01 | 2015-12-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2017-09-30 | 2018-01-31 |
> > [2017-09-30,2018-02-15)
> >          2 |       2 | 2018-02-01 | 2018-02-14 |
> > [2017-09-30,2018-02-15)
> > (17 rows)
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > 
> > 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
> > (253) 245-3801
> > 
> > 
> > Subscribe to the mailing list to
> > learn more about AGENCY or
> > follow the discussion.
> 
> 
> 






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates)
@ 2018-02-28 00:15  Ken Tanzer <ken.tanzer@gmail.com>
  parent: mariusz <marius@mtvk.pl>
  0 siblings, 1 reply; 18+ messages in thread

From: Ken Tanzer @ 2018-02-28 00:15 UTC (permalink / raw)
  To: mariusz <marius@mtvk.pl>; +Cc: pgsql-general

Hi.  Thanks so much for your assistance.  This is definitely getting the
results I was looking for.  It is still syntacticallly more cumbersome than
I might have hoped, but I can work with it.  So I've got two follow
questions/issues:

1)  I can see there are many, more complex, options for aggregates, which I
am trying to wrap my mind around.  I'm wondering if any of these (esp.
partial aggregates/combine functions, final functions or moving aggregates)
could be used to streamline this into a single function call, or if that is
barking up a dead tree.

2)  I'm sure at this point I must be being dense, but after re-reading docs
multiple times, I am still confused about one piece of this:

> first window (within lower() func) extends its initial (earliest) range
> > to right for every next row continuous with current range (and jumps to
> > new start on discontinuity), thus over(order by d ASC)
> > second window (within upper() func) extends its initial (latest) range
> > to left for every next row continuous with current range (and jumps to
> > new end on discontinuity), thus over(order by d DESC)
> > partition by client_id within each over() is to not mix client_ids'
> > dateranges
> >
>
> which is what is it exactly that is triggering Postgresql to know there is
a discontinuity and to start with a new range?  And is it based on the
input or the output values?  Based on PARTITION BY client_id ORDER BY d, I
might have thought it was d.  But that doesn't seem to be right.  So is it
something about what agg_daterange is returning, and if so what?  Again,
sorry for being dense about this one.

Thanks!

Ken


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

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

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates)
@ 2018-02-28 10:49  mariusz <marius@mtvk.pl>
  parent: Ken Tanzer <ken.tanzer@gmail.com>
  0 siblings, 0 replies; 18+ messages in thread

From: mariusz @ 2018-02-28 10:49 UTC (permalink / raw)
  To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: pgsql-general


hi,

On Tue, 2018-02-27 at 16:15 -0800, Ken Tanzer wrote:
> Hi.  Thanks so much for your assistance.  This is definitely getting
> the results I was looking for.  It is still syntacticallly more
> cumbersome than I might have hoped, but I can work with it.  So I've
> got two follow questions/issues:
> 
> 
> 1)  I can see there are many, more complex, options for aggregates,
> which I am trying to wrap my mind around.  I'm wondering if any of
> these (esp. partial aggregates/combine functions, final functions or
> moving aggregates) could be used to streamline this into a single
> function call, or if that is barking up a dead tree.
> 
i'm not an expert and i don't use fancy aggregates too often, just got a
simple solution, so i may be wrong, but i don't see simple solution as
one function. so unless someone comes with a better idea, you have to
stay with this, which isn't complex as the idea and solution is really
very simple (simplicity is somewhat visually hidden in that case by
converting dates to ranges and to boundary dates again (and again to
ranges for visualizing result)).
but it requires additional sort for each window as you can see in
explain which is the cost to pay
> 
> 2)  I'm sure at this point I must be being dense, but after re-reading
> docs multiple times, I am still confused about one piece of this:
> 
> 
>         > first window (within lower() func) extends its initial
>         (earliest) range
>         > to right for every next row continuous with current range
>         (and jumps to
>         > new start on discontinuity), thus over(order by d ASC)
>         > second window (within upper() func) extends its initial
>         (latest) range
>         > to left for every next row continuous with current range
>         (and jumps to
>         > new end on discontinuity), thus over(order by d DESC)
>         > partition by client_id within each over() is to not mix
>         client_ids'
>         > dateranges
>         >
>         
>         
> which is what is it exactly that is triggering Postgresql to know
> there is a discontinuity and to start with a new range?  And is it
> based on the input or the output values?  Based on PARTITION BY
> client_id ORDER BY d, I might have thought it was d.  But that doesn't
> seem to be right.  So is it something about what agg_daterange is
> returning, and if so what?  Again, sorry for being dense about this
> one.
> 
posgresql itself doesn't know nor care about those discontinuities, all
it cares about is partition by client_id to not mix client_ids, and
applying our agg function for rows in order of our choice.

here again i remind you, you don't really want to sort by d::daterange
for second descending window, but end_date because desc order of range
will not be the same as desc order of end_date in general case,
and start_date for first window (but as i already said, for that first
ascending window it's cosmetics as order of d::daterange and
start_date::date will be the same.
that doesn't really matter in your case of exclusive ranges but matters
in more general case.

and back to your question, we find discontinuity in our function. see
the query

SELECT CASE WHEN d1 && d2 OR d1 -|- d2 THEN d1 + d2 ELSE d2 END

where d1 is internal state value of aggregate and d2 is current row's
daterange value.
we check for continuity by d1 && d2 OR d1 -|- d2, and if it is
continuous than we just extend result (return value for current row and
new state value of agg) by d1+d2,
otherwise, when we find discontinuity, we forget d1 (agg state value
until now) and simply return d2 being current row's range, thus starting
with new range.

hope that explains enough. as i already said, i'm not an expert, i'm
just coincidentally working currently on my semi-toy project which
utilizes dateranges quite heavily.
anyway, feel free to ask if you have any further questions. for now i'm
glad i could help somehow.

regards,
mariusz jadczak

> 
> Thanks!
> 
> 
> Ken
> 
> 
> 
> -- 
> 
> 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
> (253) 245-3801
> 
> 
> Subscribe to the mailing list to
> learn more about AGENCY or
> follow the discussion.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread


end of thread, other threads:[~2018-02-28 10:49 UTC | newest]

Thread overview: 18+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-23 00:44 Given a set of daterange, finding the continuous range that includes a particular date Ken Tanzer <ken.tanzer@gmail.com>
2018-02-23 00:53 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-02-23 00:58   ` Ken Tanzer <ken.tanzer@gmail.com>
2018-02-23 01:05     ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-02-23 01:23       ` Ken Tanzer <ken.tanzer@gmail.com>
2018-02-23 01:47         ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-02-23 05:05         ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-02-23 05:08           ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-02-23 14:10         ` mariusz <marius@mtvk.pl>
2018-02-23 05:45 ` Paul Jungwirth <pj@illuminatedcomputing.com>
2018-02-23 14:42 ` Viktor Fougstedt <viktor@chalmers.se>
2018-02-24 01:07   ` Ken Tanzer <ken.tanzer@gmail.com>
2018-02-24 02:11 Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates) Ken Tanzer <ken.tanzer@gmail.com>
2018-02-27 09:03 ` Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates) mariusz <marius@mtvk.pl>
2018-02-27 09:16   ` Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates) mariusz <marius@mtvk.pl>
2018-02-27 11:26   ` Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates) mariusz <marius@mtvk.pl>
2018-02-28 00:15     ` Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates) Ken Tanzer <ken.tanzer@gmail.com>
2018-02-28 10:49       ` Re: Given a set of daterange, finding the continuous range that includes a particular date (aggregates) mariusz <marius@mtvk.pl>

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