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 1ep6BN-0002JX-Qp for pgsql-general@arkaria.postgresql.org; Fri, 23 Feb 2018 05:46:10 +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 1ep6BK-00065s-VI for pgsql-general@arkaria.postgresql.org; Fri, 23 Feb 2018 05:46:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ep6BK-00065d-JT for pgsql-general@lists.postgresql.org; Fri, 23 Feb 2018 05:46:06 +0000 Received: from mail-io0-x22e.google.com ([2607:f8b0:4001:c06::22e]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ep6BB-0000av-ND for pgsql-general@lists.postgresql.org; Fri, 23 Feb 2018 05:46:05 +0000 Received: by mail-io0-x22e.google.com with SMTP id n7so8619387iob.0 for ; Thu, 22 Feb 2018 21:45:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=JxW04lWx88UDdTl+qNvQRdm8zMofoHIiKmnS6PsfWOY=; b=OZBdZooBPEBiWRMSA1DlAXriK4XxmXyBC54POu8TcvxcoHHUMkwkP8i3Sva7ccuuoB Ooss/UfUMdD3nsiohCK9fcTeIIzCol5fHYtBDxyzdSx1aF3sJ/lT1vAEo/eyUR3Z0c27 dzVAHAvPXXuXBNaXVkNAxftmjaaQmn2e2AYEi2Qzi61JZy1150ecxSb+Xtlb525medPP 3KvBeygQTiHIy9QD8QaOqukZ0GpgEn8Z6JKjxxWnpbg7hKvCXEfeyz+sI3i3Wb/qUgVK 7JXia0io0g9QUHE8jHU9Af9gUzvUoVK4s3yh3xozwRHf0K1p5Su98/aLjSAktmlJlLuQ VWbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=JxW04lWx88UDdTl+qNvQRdm8zMofoHIiKmnS6PsfWOY=; b=DuEGpUUu74JDzANjOkBhOyL8Y1YvQ5jlhja8VH+dGuDLiW3K+sKmJ4N3W0F9Qmts2b ZreNY5a9+qGHIT0FsovV9iRyKyz3F3KyiK83DV4f8enBlhw7W73j2+Kgn1Oi/O8BX4vi pOdt2WocmvM13HqDnxlh7FLEU3FVt2yjYsCvGu8tsCa18retO0aijcwQbK/A1OPb0ycI 08CaxxD6d+Mqms9BD1cIpjvX1Ael5FrQi+hCVekg9wnW4P13Pv/Ih2Et5sA0XRsgEcql OA2lBF95atW3FcS3+fSL35AE8hAbcwIZrQWEO4kBczmZym52b+gva2v8TPw4XiHO7Rt5 5aMQ== X-Gm-Message-State: APf1xPBCdX7qBG1ZnAwGgvWVzaRVPVKEaVnpzslGg1Dsivt3v9/KImwS uF/ZHFHnsBL7a3hbdp+JjZK+qzj3 X-Google-Smtp-Source: AH8x224mOJ4Z5aK4xTp8C7d2sPdtSeSKZ7RjeaqJbVsiUis+5kjMw4BNPlIJrkGJuHIhTX8bT2vanQ== X-Received: by 10.107.167.85 with SMTP id q82mr571034ioe.82.1519364754266; Thu, 22 Feb 2018 21:45:54 -0800 (PST) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id i201sm735430ita.14.2018.02.22.21.45.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 21:45:53 -0800 (PST) Subject: Re: Given a set of daterange, finding the continuous range that includes a particular date To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: Date: Thu, 22 Feb 2018 21:45:51 -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. 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