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 1erKje-00056f-JJ for pgsql-sql@arkaria.postgresql.org; Thu, 01 Mar 2018 09:42:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erKjd-0008PT-MA for pgsql-sql@arkaria.postgresql.org; Thu, 01 Mar 2018 09:42:45 +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.89) (envelope-from ) id 1erKjd-0008He-Bx for pgsql-sql@lists.postgresql.org; Thu, 01 Mar 2018 09:42:45 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erKja-0003QU-2y for pgsql-sql@postgresql.org; Thu, 01 Mar 2018 09:42:44 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtp (Exim 4.89) (envelope-from ) id 1erKjW-000BWg-Cx for pgsql-sql@postgresql.org; Thu, 01 Mar 2018 09:42:39 +0000 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Re: Monthly budgets Date: Thu, 1 Mar 2018 09:42:38 +0000 User-Agent: KMail/1.9.10 References: <201803010842.19235.gary.stainburn@ringways.co.uk> In-Reply-To: <201803010842.19235.gary.stainburn@ringways.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201803010942.38097.gary.stainburn@ringways.co.uk> X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 122114 [Mar 01 2018] X-KLMS-AntiSpam-Version: 5.7.102.0 X-KLMS-AntiSpam-Envelope-From: gary.stainburn@ringways.co.uk X-KLMS-AntiSpam-Rate: 0 X-KLMS-AntiSpam-Status: not_detected X-KLMS-AntiSpam-Method: none X-KLMS-AntiSpam-Info: LuaCore: 103 103 74d8b0efd27dea143cb5f03853293d8f76b15378, {msgid_created_by_recepient}, ringways.co.uk:7.1.1;eddie.ringways.co.uk:7.1.1;127.0.0.199:7.1.2;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1, DmarcAF: none X-KLMS-AntiSpam-Interceptor-Info: scan successful X-KLMS-AntiPhishing: Clean, 2018/02/27 12:19:13 X-KLMS-AntiVirus: Kaspersky Security 8.0 for Linux Mail Server, version 8.0.1.721, bases: 2018/03/01 00:46:00 #12077572 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Score: -50.8 (--------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: I already had a date_range function, so was able to do select distinct date_trunc('month',date_range)::date as month_start from date_range('2017-01-01'::date,'2018-02-20'::date) which gives me a more flexible version of the month_starts function. [...] Content analysis details: (-50.8 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.1 SCORE_RCPTS Adding score for each recipient -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE No description available. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I already had a date_range function, so was able to do select distinct date_trunc('month',date_range)::date as month_start from date_range('2017-01-01'::date,'2018-02-20'::date) which gives me a more flexible version of the month_starts function. select distinct date_trunc('month',date_range)::date as month_start from date_range('2017-07-01'::date,'2018-02-20'::date); month_start ------------- 2017-07-01 2017-08-01 2017-09-01 2017-10-01 2017-11-01 2017-12-01 2018-01-01 2018-02-01 (8 rows) I then managed to create a select to create the dataset that I want. select d.dept_id, d.month_start, coalesce(a.target_units,d.target_units) as target_units, coalesce(a.unit_cost,d.unit_cost) as unit_cost from (select d.*, month_start from default_bugdet d, (select distinct date_trunc('month',date_range)::date as month_start from date_range('2017-09-01'::date,'2018-08-01'::date)) as c ) as d left outer join adjustments a on a.month_start = d.month_start and a.dept_id = d.dept_id order by month_start; dept_id | month_start | target_units | unit_cost ---------+-------------+--------------+----------- 1 | 2017-09-01 | 20 | 10.00 1 | 2017-10-01 | 20 | 10.00 1 | 2017-11-01 | 20 | 10.00 1 | 2017-12-01 | 20 | 10.00 1 | 2018-01-01 | 15 | 10.00 1 | 2018-02-01 | 20 | 15.00 1 | 2018-03-01 | 20 | 10.00 1 | 2018-04-01 | 20 | 10.00 1 | 2018-05-01 | 20 | 10.00 1 | 2018-06-01 | 20 | 10.00 1 | 2018-07-01 | 20 | 10.00 1 | 2018-08-01 | 20 | 10.00 (12 rows) Is there a better way of achieving this? Is there a more efficient query I could use? Is there any way to create a rule for this instead of creating a function?