Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id A0B8443CEB7 for ; Thu, 19 Jan 2012 03:14:05 -0400 (AST) Received: from hub.ringways.co.uk ([77.86.27.30] helo=mail.ringways.co.uk) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RnmCB-000241-8t for pgsql-sql@postgresql.org; Thu, 19 Jan 2012 07:14:05 +0000 Received: from localhost ([127.0.0.1] helo=mail.ringways.co.uk) by mail.ringways.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1RnmBu-0005eU-8O for pgsql-sql@postgresql.org; Thu, 19 Jan 2012 07:13:48 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id q7DXuGGZ45104; Thu, 19 Jan 2012 07:13:33 +0000 From: Gary Stainburn Organization: Ringways Garages Ltd To: "pgsql-sql@postgresql.org" Subject: date range to set of dates expansion Date: Thu, 19 Jan 2012 07:13:34 +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: <201201190713.34333.gary.stainburn@ringways.co.uk> X-SpamTest-Envelope-From: gary.stainburn@ringways.co.uk X-SpamTest-Info: Profiles 20401 [Mar 31 2011] X-SpamTest-Info: {TO: suspicious real name} X-SpamTest-Method: none X-SpamTest-Rate: 10 X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0285], KAS30/SDK/Release X-Anti-Virus: Kaspersky Mail Gateway, version: 5.6.28/RELEASE, bases: 20110331T110535 #5151353, check: 20120119 clean X-Spam-Score: -51.5 (---------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie.ringways.co.uk", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: Hi, How can I expand a date range in a table to a set of date records? I have a table of availabilities thus: [...] Content analysis details: (-51.5 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE RING_SAFE 0.1 AWL AWL: From: address is in the auto white-list X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/84 X-Sequence-Number: 36255 Hi, How can I expand a date range in a table to a set of date records? I have a table of availabilities thus: Column | Type | Modifiers -----------+-----------------------------+------------------------------------------------------------ aid | integer | not null default nextval('availability_aid_seq'::regclass) asid | integer | not null asdate | date | not null afdate | date | not null adays | integer | acomments | text | asdate is the start date afdate is the finish date How can I expand this to a set of Column | Type | Modifiers -----------+-----------------------------+------------------------------------------------------------ aid | integer | not null asid | integer | not null adate | date | not null acomments | text | i.e. aid | asid | asdate | afdate | adays | acomments -----+------+------------+------------+-------+-------------------- 12 | 1 | 2007-08-11 | 2007-08-12 | 1 | Early finish Sunday Becomes aid | asid | asdate | acomments -----+------+------------+-------------------- 12 | 1 | 2007-08-11 | Early finish Sunday 12 | 1 | 2007-08-12 | Early finish Sunday I have a function date_range to return a set of dates, but so far I can't get a valid view to work. Also, is there a better method? CREATE FUNCTION date_range(fdate date, tdate date) RETURNS SETOF date AS $$ DECLARE wdate date; BEGIN return next fdate; wdate:=fdate+1; while wdate <= tdate LOOP return next wdate; wdate:=wdate+1; end LOOP; return; END; $$ LANGUAGE plpgsql; -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk