pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: C. Bensend <benny@bennyvision.com>
To: pgsql-sql@postgresql.org
Subject: Returning a set of dates
Date: Fri, 10 Jun 2011 23:28:04 -0500
Message-ID: <c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net> (raw)


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





view thread (3+ messages)  latest in thread

Message-ID: <c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net>
Permalink:  ../c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net/
Also on:    postgresql.org/message-id/c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: benny@bennyvision.com
  Subject: Re: Returning a set of dates
  In-Reply-To: <c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox