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 1ep1cI-000496-5Z for pgsql-general@arkaria.postgresql.org; Fri, 23 Feb 2018 00:53:38 +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 1ep1cF-0005GK-Ma for pgsql-general@arkaria.postgresql.org; Fri, 23 Feb 2018 00:53:35 +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 1ep1cE-0005G2-W0 for pgsql-general@lists.postgresql.org; Fri, 23 Feb 2018 00:53:35 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ep1bs-0001jF-4p for pgsql-general@postgresql.org; Fri, 23 Feb 2018 00:53:33 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6C11E21498; Thu, 22 Feb 2018 19:53:09 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Thu, 22 Feb 2018 19:53:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= 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=g2fQuRcOHA5go9PBr8vqfaehVhKtL EbEDKYe0SRPOQw=; b=n1REjGdjuYevrjq0HNY573WGA1QL2x8MN2rsT3VIwEq/m OoC+yWpDbRrgJTMYyf0UD3C8wwJaSswyCASO6zylNKy/uUsjx+iUOfj7GO7N2LDh 9uHi+7B4OR1lW51Ww7iT9c6mLK2BC2nhaZnrDWhZJpFN6SjWkOmZz+mX4yOKpele qhIGUR/20MSYmXRIeZAR7/KP9E+NeBnxpMX+BRT53orKulqfS2/t6Gqb1EZlBfKe gIcrC3ywW4KQB8tIFAX5biYrEz9tZeEaph8NItgay8H8eVSOOgCMEy/7ps3K7ExV pIILAN3eLLJBO1eU7TyD4FWMVTxZPI9sXYRZU9HVg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=g2fQuR cOHA5go9PBr8vqfaehVhKtLEbEDKYe0SRPOQw=; b=Ar5UDEkS8bukqeSuMZVyFU L5CGiIDCIaHJOsxwWZNxbzHgey+vppblBENC45tjushop3MAcvWx9pKqv4mFbtOn ir72LaN5+CUQa/6LLduqanGSXtio24kyYm2dK7SxRFaPKP5CCNVXIODcmAkdxaut 6eehkuX1KVM8RsPRUVQ5nyAu8LmHtw+cKT0/6WomKhovehVrgTh7Fh2TZMKbxe4g cGPlU+xFgXZJX1K4NWfeDD+dY50jawCyoibPoHvg6JXl85U9VEfqV6uxIGJMoaQK I9XcPNKJt1Qx8boxmsRRLBAF5ocCG7yeh4cvm1ZBThjfVp2iSkmfH6XPABQd6+zQ == 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 D272B7E17D; Thu, 22 Feb 2018 19:53:08 -0500 (EST) Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date To: Ken Tanzer , PG-General Mailing List References: From: Adrian Klaver Message-ID: <8a557b3d-a065-aea2-7d20-d165d96a08d8@aklaver.com> Date: Thu, 22 Feb 2018 16:53:07 -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 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 > (253) 245-3801 > > Subscribe to the mailing list >  to > learn more about AGENCY or > follow the discussion. -- Adrian Klaver adrian.klaver@aklaver.com