Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 5675CB5DBBE for ; Sat, 11 Jun 2011 01:36:25 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 08187-05 for ; Sat, 11 Jun 2011 04:36:18 +0000 (UTC) X-Greylist: delayed 00:08:11.729509 by SQLgrey-1.7.6 Received: from fusion.bennyvision.com (fusion.bennyvision.com [207.158.15.45]) by mail.postgresql.org (Postfix) with ESMTP id 4CCB3B5D80A for ; Sat, 11 Jun 2011 01:36:18 -0300 (ADT) Received: from webmail.stinkweasel.net (localhost [127.0.0.1]) by fusion.bennyvision.com (Postfix) with ESMTP id 8FB241E87A6 for ; Fri, 10 Jun 2011 23:28:04 -0500 (CDT) Received: from 173.28.21.46 (SquirrelMail authenticated user benny) by webmail.stinkweasel.net with HTTP; Fri, 10 Jun 2011 23:28:04 -0500 Message-ID: Date: Fri, 10 Jun 2011 23:28:04 -0500 Subject: Returning a set of dates From: "C. Bensend" To: pgsql-sql@postgresql.org User-Agent: SquirrelMail/1.4.21 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: clamav-milter 0.96.1 at fusion.bennyvision.com X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/31 X-Sequence-Number: 35497 Hey folks, I am still slogging away on my pet project, and I'm giving up - I need help from the experts to try to get this function working like I want it to. I have a very basic function, thanks to you guys a few months ago: CREATE OR REPLACE FUNCTION public.next_bill_date(d date, period interval, i interval) RETURNS date LANGUAGE plpgsql AS $function$ DECLARE ret DATE; BEGIN -- We must use "CURRENT_DATE + period" in SELECT below - if we just -- use "d + i", we will only get the next billing date after the -- bill's *epoch*. Since we're passing an epoch, we need to -- make sure we're getting the real next billing date SELECT INTO ret generate_series( d, CURRENT_DATE + period, i ) AS next_bill_date ORDER BY next_bill_date DESC; RETURN ret; END; $function$ If I call the function like so: SELECT next_bill_date( '2011-06-10', '1 month', '1 year' ); next_bill_date ---------------- 2011-06-10 .. it gives me the *next date* a bill will be due. Yay. However, several of my bills are recurring in a time period shorter than the interval I will pass the function. Ie, I have several savings "bills" that occur twice a month, aligned with my paycheck. If I call the function: SELECT next_bill_date( '2011-06-01', '2 weeks', '1 month' ) .. I need it to return the two dates during the interval (1 month) that this "bill" will be due. I am brain-weary looking at this, so in case my explanation is not clear, here's what I'm shooting for: 1) The function must accept an epoch date, when the bill "starts". Ie, my mortgage's epoch is on 2011-01-01, as it's due on the 1st of the month. The month and year aren't as critical, they just need to represent a "starting date" in the past. This epoch could just as well be '2011-06-01'. I manually enter the epochs, so I can make the assumption that it will always be in the past. 2) The function must accept a period, or how often the bill recurs. So, most bills will have a period of '1 month'. Some might be '2 weeks'. Some, like insurance, might be '6 months' or even '1 year'. 3) The function must accept an interval, describing how long of a time period we want to look at. Ie, "I want to look at all bills over the next six months." The interval would be '6 months'. Or the upcoming bills over '6 weeks'. You get the idea. So, for example, if I call the function to determine my mortgage's due dates over the next four months: SELECT next_bill_date( '2011-01-01', '1 month', '4 months' ); .. I expect the following result set: next_bill_date ---------------- 2011-07-01 2011-08-01 2011-09-01 2011-10-01 I know I must use SETOF to return the set. But I just can't seem to get the damned syntax correct, and I'm more than a little lost trying to get this function put together. Can someone please help me out? Thanks much! Benny -- "You were doing well until everyone died." -- "God", Futurama