Returning a set of dates

From: "C(dot) Bensend" <benny(at)bennyvision(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Returning a set of dates
Date: 2011-06-11 04:28:04
Message-ID: c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Samuel Gendler 2011-06-11 05:11:37 Re: Returning a set of dates
Previous Message Emi Lu 2011-06-10 15:10:21 ANY for Array value check