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 1erJnG-0000uo-Hi for pgsql-sql@arkaria.postgresql.org; Thu, 01 Mar 2018 08:42:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erJnF-00050F-4K for pgsql-sql@arkaria.postgresql.org; Thu, 01 Mar 2018 08:42:25 +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 1erJnE-000505-Tv for pgsql-sql@lists.postgresql.org; Thu, 01 Mar 2018 08:42:24 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erJnB-0001ad-S3 for pgsql-sql@postgresql.org; Thu, 01 Mar 2018 08:42:24 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtp (Exim 4.89) (envelope-from ) id 1erJn9-000906-NU for pgsql-sql@postgresql.org; Thu, 01 Mar 2018 08:42:20 +0000 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Monthly budgets Date: Thu, 1 Mar 2018 08:42:19 +0000 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201803010842.19235.gary.stainburn@ringways.co.uk> X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 122113 [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;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2;eddie.ringways.co.uk: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 have two tables create table default_bugdet ( dept_id int4 primary key, target_units int4 not null, unit_cost numeric(9,2) ); create table adjustments ( dept_id int4 not null, month_start date not null, target_units int4, unit_cost numeric(9,2), primary key (dept_id,month_start) ); [...] 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 have two tables create table default_bugdet ( dept_id int4 primary key, target_units int4 not null, unit_cost numeric(9,2) ); create table adjustments ( dept_id int4 not null, month_start date not null, target_units int4, unit_cost numeric(9,2), primary key (dept_id,month_start) ); I have test data: goole=# select * from default_bugdet ; dept_id | target_units | unit_cost ---------+--------------+----------- 1 | 20 | 10.00 (1 row) goole=# select * from adjustments ; dept_id | month_start | target_units | unit_cost ---------+-------------+--------------+----------- 1 | 2018-02-01 | | 15.00 1 | 2018-01-01 | 15 | (2 rows) If I use the following select it appears to give me what I want. goole=# select d.dept_id, a.month_start, coalesce(a.target_units,d.target_units) as target_units, coalesce(a.unit_cost,d.unit_cost) as unit_cost from default_bugdet d, adjustments a where d.dept_id=a.dept_id order by month_start; dept_id | month_start | target_units | unit_cost ---------+-------------+--------------+----------- 1 | 2018-01-01 | 15 | 10.00 1 | 2018-02-01 | 20 | 15.00 (2 rows) However, how can I create a view that would return: dept_id | month_start | target_units | unit_cost ---------+-------------+--------------+----------- 1 | 2018-01-01 | 15 | 10.00 1 | 2018-02-01 | 20 | 15.00 1 | 2018-03-01 | 20 | 10.00 .. 1 | 2018-12-01 | 20 | 10.00 (12 rows) I've through about using date_trunc and a range but can't work out how to actually generate the dataset to do the date_trunc on. I've come up with the following function which creates the dataset,but have no idea how I would create a view from it. Would I have to create another function that returns a setof default_budget? create or replace FUNCTION month_start(year int4) RETURNS SETOF date AS $$ DECLARE wdate date; i int4; BEGIN FOR i in 1..12 LOOP select (year::text || '-' || i::text || '-01'::text)::date into wdate; return next wdate; end LOOP; return; END; $$ LANGUAGE plpgsql; select * from month_start(2018); month_start ------------- 2018-01-01 2018-02-01 2018-03-01 2018-04-01 2018-05-01 2018-06-01 2018-07-01 2018-08-01 2018-09-01 2018-10-01 2018-11-01 2018-12-01 (12 rows)