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 1f74jD-0000qH-MZ for pgsql-general@arkaria.postgresql.org; Fri, 13 Apr 2018 19:51:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f74jB-0008LX-Oz for pgsql-general@arkaria.postgresql.org; Fri, 13 Apr 2018 19:51:21 +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.89) (envelope-from ) id 1f74jA-0008LN-LT for pgsql-general@lists.postgresql.org; Fri, 13 Apr 2018 19:51:21 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f74j1-000129-Jf for pgsql-general@postgresql.org; Fri, 13 Apr 2018 19:51:20 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 7306821B34; Fri, 13 Apr 2018 15:51:09 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 13 Apr 2018 15:51:09 -0400 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=Z93xLDjWRvu9ZTseuQyJ6lTzu+1oa /nHgZXZRd+OdTk=; b=c4CEN2LWknCwv8DDqGn6dprMGtw5eWjMxzKU3gG/JYXvd 8RxPERA4wPZKrjKvj/U5fSl2IH+s3uZljR/sXwMu4vXjgm21yxegelXRrY5nd2D0 nGWN4oniBZ+OG36KhWCPKU4j5JdkAaOCM9JoTSo4UYNWxOfSBK/DUmAM1Cc80Cz2 AcIsFH64eM02C6bHNIRCcJHWrvWgWxu/OXApKDcsfIUEhUVgxHlZqeLH2F4sRkaj j9v5PvQjp1nFUb6U+Mzp6TVKyhQgpeZDg+I0WK0cpjMUdHLgo5m3nOAPc8253zAk WQwpTdLgFlWV4Pb36yOO66nVzQg1QxtbmwXYOOtEw== 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=Z93xLD jWRvu9ZTseuQyJ6lTzu+1oa/nHgZXZRd+OdTk=; b=mgXaly0l7/jsBNlQCZSOFU G67jWx/cWVaBM6SlBLXdr85lG0oq3HPp8X9TVfDPHq36P5TrkJ4nacBX6o6kdRJt OEoNSj4J1reRZXodQcjxKqCqqgBNv2hodnhM9kF9/MfUGKTvnstqf6Sy5i1CltbU jQL9/oQxxdNRqPo1SR6CVTk5IB41pZpR/OSM+QHIiS2VNB+2wldQ/naYEz+m6+T2 oS6RiWGVMV1P5XU+N/Z0Gf5IENl16NKbA9eEC+trjc+I4g07bt7UgyiSGYXcDuuy +vvj9xxBK8gDq+/T09aksiW0zUzXI+9pusOnv701lgYH9SFg/tW4a2Pnimy2labg == 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 D7D871027E; Fri, 13 Apr 2018 15:51:08 -0400 (EDT) Subject: Re: Table schema inhancement To: hmidi slim , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: Date: Fri, 13 Apr 2018 12:51:07 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/12/2018 05:59 PM, hmidi slim wrote: > Hi, > I have these tables: > price_per_occupation: id (integer), product_price_period_id(integer), > occupation_type(integer), price (numeric) > > product_price_period; id(integer), product_id(integer), > is_monday(boolean), is_tuesday(boolean), is_wednesday(boolean), > is_thursday(boolean), is_friday(boolean), is_saturday(boolean), > is_sunday(boolean), price_period(daterange) > > occupation_type: id(integer), name(varchar) > > product: id(integer), name(varchar) > > I run this query: > /*select price_per_occupation.price, product_price_period.price_period, > occupation_type.name > */ > /*from price_per_occupation inner join product_price_period on > product_price_period.id = > price_per_occupation.product_price_period_id > */ > /*inner join occupation_type on occupation_type.id > = price_per_occupation.occupation_type > */ > /*inner join product on product.id = > product_price_period.product_id > */ > /*where product_price_period.price_period @> '[2018-07-22, 2018-07-23]' > */ > /*and occupation_type.id = 1*/ > > > This query returns all the products with an occupation_type = 1 and have > the period_price between 2018-07-22 and 2018-07-23. > > However I need to verify if the boolean values verified. > E.g if is_monday = true the date corresponding to Monday will be > eliminated from the period. > if is_sunday = true and is_friday = true the dates corresponding to > Sunday and Friday will be eliminated from the period interval. > If I will choose all the products in the interval > [2018-04-07,2018-04-14] and is_monday = true and is_thursday= true > the date of monday is 09/04/2018 and date of friday is 13/04/2018. > I have to get all products contained in [2018-04-07,2018-04-08] U > [2018-04-10, 2018-04-12] U [2018-04-14, 2018-04-14] > > In order to get the date of truthy columns I should execute a function > which contains a query like that: > > |select * > | > |from generate_series(date '2018-04-07', date '2018-04-14', interval '1 > day') the day > | > |where extract ('dow', the_day) = 1 > > > | > |I'm face many problems with this schema; > | > |I should verify the boolean values. > | > |I should extract the corresponding dates based to the values. > | > |I decompose the period into different periods then run the query to > fetch the products. > > | > |How can I enhance the query? or is there any proposition for the schema > table to enhance it? My 2 cents eliminate the is_* fields and create a single field: dow_verified integer[] Then assuming non-iso week day number Sunday(0) --> Saturday(6) in the array enter the day of week(dow) numbers for verified days e.g.: ARRAY[0, 1, 4] for Sunday, Monday, Thursday. Then to get the days that are not verified over a period something like: WITH dp AS ( SELECT extract('dow' FROM generate_series('04/01/18'::date, '04/14/18'::date, '1 day')) AS dow, generate_series('04/01/18'::date, '04/14/18'::date, '1 day') AS dt ) SELECT dp.* FROM dp WHERE dp.dow NOT IN ( ( SELECT * FROM unnest(ARRAY [ 0, 1, 4 ]) AS dow_dt) ) ORDER BY dt; dow | dt -----+------------------------ 2 | 2018-04-03 00:00:00-07 3 | 2018-04-04 00:00:00-07 5 | 2018-04-06 00:00:00-07 6 | 2018-04-07 00:00:00-07 2 | 2018-04-10 00:00:00-07 3 | 2018-04-11 00:00:00-07 5 | 2018-04-13 00:00:00-07 6 | 2018-04-14 00:00:00-07 (8 rows) > | > || > || > > > -- Adrian Klaver adrian.klaver@aklaver.com