Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRDRr-0007bw-GT for pgsql-general@arkaria.postgresql.org; Thu, 16 May 2019 10:17:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hRDQr-0000Ey-Sn for pgsql-general@arkaria.postgresql.org; Thu, 16 May 2019 10:16:13 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRDQr-0000E6-In for pgsql-general@lists.postgresql.org; Thu, 16 May 2019 10:16:13 +0000 Received: from mail.i-mark.de ([188.138.104.222]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRDQo-000067-V7 for pgsql-general@lists.postgresql.org; Thu, 16 May 2019 10:16:13 +0000 Received: from [192.168.178.20] (unknown [185.38.49.7]) by mail.i-mark.de (Postfix) with ESMTPSA id ACA561583978 for ; Thu, 16 May 2019 12:16:06 +0200 (CEST) Authentication-Results: loft7616.startdedicated.de; spf=pass (sender IP is 185.38.49.7) smtp.mailfrom=andreas@a-kretschmer.de smtp.helo=[192.168.178.20] Received-SPF: pass (loft7616.startdedicated.de: connection is authenticated) Subject: Re: How to search using daterange (using gist) To: pgsql-general@lists.postgresql.org References: From: Andreas Kretschmer Message-ID: Date: Thu, 16 May 2019 12:16:08 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Am 16.05.19 um 11:57 schrieb Winanjaya Amijoyo: > Hi All, > > I have records as below that I inserted using exclusion gist constraint > > user_id    start_date      end_date         pid > 001          2019-01-01    2019-02-10        1 > 001          2019-02-01    2019-03-12        2 > 001          2019-03-05    2019-06-15        3 > > I need to find records based on date range, so for example: > > if I search using parameters 001, 2019-01-10, 2019-02-11 .. it will > found as pid 1 > if I search using parameters 001, 2019-03-10, 2019-07-13 .. it will > found as pid 3 > but if I search out of the date range, for example: > if I search using parameters 001, 2019-07-10, 2019-09-13 .. it will > not found as pid 0 > > how to search with the above scenario in postgresql? > > can you please show us the table-definition? Are you sure there is an exclusion constraint? with your data: test=*# select * from demo;  user_id | start_date |  end_date  | pid ---------+------------+------------+-----        1 | 2019-01-01 | 2019-02-10 |   1        1 | 2019-02-01 | 2019-03-12 |   2        1 | 2019-03-05 | 2019-06-15 |   3 (3 rows) test=*# select * from demo where daterange(start_date, end_date,'[)') && daterange('2019-01-10','2019-02-11');  user_id | start_date |  end_date  | pid ---------+------------+------------+-----        1 | 2019-01-01 | 2019-02-10 |   1        1 | 2019-02-01 | 2019-03-12 |   2 (2 rows) test=*# select * from demo where daterange(start_date, end_date,'[)') && daterange('2019-03-10','2019-07-13');  user_id | start_date |  end_date  | pid ---------+------------+------------+-----        1 | 2019-02-01 | 2019-03-12 |   2        1 | 2019-03-05 | 2019-06-15 |   3 (2 rows) test=*# select * from demo where daterange(start_date, end_date,'[)') && daterange('2019-07-10','2019-09-13');  user_id | start_date | end_date | pid ---------+------------+----------+----- (0 rows) test=*# test=*# \d demo                   Table "public.demo"    Column   |  Type   | Collation | Nullable | Default ------------+---------+-----------+----------+---------  user_id    | integer |           |          |  start_date | date    |           |          |  end_date   | date    |           |          |  pid        | integer |           |          | test=*# Regards, Andreas -- 2ndQuadrant - The PostgreSQL Support Company. www.2ndQuadrant.com