Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ep5YM-0000Me-9j for pgsql-general@arkaria.postgresql.org; Fri, 23 Feb 2018 05:05:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ep5YL-00042m-6f for pgsql-general@arkaria.postgresql.org; Fri, 23 Feb 2018 05:05:49 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ep5YK-00042c-LK for pgsql-general@lists.postgresql.org; Fri, 23 Feb 2018 05:05:49 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ep5YG-0007FJ-Qo for pgsql-general@postgresql.org; Fri, 23 Feb 2018 05:05:47 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 4D5E620FF2; Fri, 23 Feb 2018 00:05:43 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Fri, 23 Feb 2018 00:05:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=T9yDlKHqbZIbn1wERuZB2bOqZXDjT iKOK1npozIGXJc=; b=ca9ZpY+b1DjUPrYLQycj356rggOFNIvaBi6YE/jc+OZB2 wbpcKGbifAYSDpIkZwmYbhSxvIT3a7LDP3WwT+tRajdn0IfGdISj8dmJs6IS/vpP SujkVEkbnjZ73tYmZNmSFYZ70fjYIeKwa3NQiPZmZgtaGjIokKHh2s1+POlbBwir xXyJBlkV3nKrMCCf2ouBueTTOt1AHpQ58bV+kzxjddC0VL2n8NAE1Z6B0akuSIhv Ta33XmjlchP6zXpRO+fnXarRLZVQWoPBqSySvIJ08ZZWzFOZIHOV6BsKgmu4HUba B7FB639IQ8MOeWfPORLDtqEhgCq6zUAePfYuS0nrA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=T9yDlK HqbZIbn1wERuZB2bOqZXDjTiKOK1npozIGXJc=; b=FiH6zPbwSg01t+WN53VBke G40ejKbdSaeudTag909Rn7Ms4NPBePh8VS9H/90Ti6FHxivKNl7NdQF7SCl5C1fy mrm/qxMpaCEIASHiWya0c4gH7ADBDhu9loG1nLlcrdohE2fGPbCf+l1jh/Tkpd+l yqyyJ/GJqmIWI6RlEmxaW4sGvgIMRUKhM7qXM8Q4wAXxY+bV6W9iqeRiZUZjcy8D hB3pffUZjOz/TF9fIXunptFtMS4CQyFw1Wl+yXBdP1FN82fex0ZSmB4rrDIqHb77 L01YBj1MoEYbUb4ZiWf8FiA+ds4qqVHGxv1FbBp0MRGiH8Wq8zVXPTq88kq2+W4Q == X-ME-Sender: Received: from [192.168.1.2] (71-35-123-124.tukw.qwest.net [71.35.123.124]) by mail.messagingengine.com (Postfix) with ESMTPA id B5E8F7E51D; Fri, 23 Feb 2018 00:05:42 -0500 (EST) Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date To: Ken Tanzer Cc: PG-General Mailing List References: <8a557b3d-a065-aea2-7d20-d165d96a08d8@aklaver.com> <05fc028d-62bf-372a-a24e-488b5f701fe3@aklaver.com> From: Adrian Klaver Message-ID: <9149730a-55ab-7a2f-6a06-85d9be2eaa4b@aklaver.com> Date: Thu, 22 Feb 2018 21:05:41 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/22/2018 05:23 PM, Ken Tanzer wrote: > > > On Thu, Feb 22, 2018 at 5:05 PM, Adrian Klaver > > wrote: > > On 02/22/2018 04:58 PM, Ken Tanzer wrote: > > > > On Thu, Feb 22, 2018 at 4:53 PM, Adrian Klaver > > >> 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? > > > 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 > (253) 245-3801 > > Subscribe to the mailing list >  to > learn more about AGENCY or > follow the discussion. -- Adrian Klaver adrian.klaver@aklaver.com