Re: SQL Question - "Recursion"

From: Guy Rouillier <guyr-ml1(at)burntmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Question - "Recursion"
Date: 2008-08-26 23:39:08
Message-ID: 48B4941C.8040100@burntmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

ktr73 wrote:
> Hi -
>
> Was wondering if anyone could help / had some thoughts. I am building
> a model for a client, and right now doing customer attrition
> modeling. Basically, the number of customers in this period is equal
> to:
>
> Beg # Customers
> + customers added this period
> - attrition
> Ending # Customers
>
> Obviously getting the beginning number of customers and # of added has
> been easy via SQL. Also, the above is fairly straightforward in a
> spreadsheet program like Excel. But I can't seem to come to grips
> with how to model the attrition line item in SQL, as it's based on
> last months ending balance ... which in turn is based on beg + add -
> attr = end for the previous period, and so on.
>
> So it seems to be a recursive function as far as I can tell ... any
> idea how to model this via SQL? I can get running totals for the # of
> adds with a correlated subquery, but I can't seem to figure out how to
> build the attrition into that running total.

Don't understand why you need recursion. Seems like attrition can be
calculated. You say you can get the beginning number of customers. The
ending number of customers for month N = beginning number of customers
for month N+1. So, if you have the beginning, ending and # customers
added, then attrition = ending - beginning - #added.

--
Guy Rouillier

In response to

Browse pgsql-general by date

  From Date Subject
Next Message steve 2008-08-27 00:19:42 Re: PITR - base backup question
Previous Message Richard Broersma 2008-08-26 22:53:33 PITR - base backup question