SQL Question - "Recursion" - Mailing list pgsql-general

From ktr73
Subject SQL Question - "Recursion"
Date
Msg-id 73fd3f1d-bc21-48ab-95e7-00ccdef8d9a3@m36g2000hse.googlegroups.com
Whole thread Raw
Responses Re: SQL Question - "Recursion"  (Guy Rouillier <guyr-ml1@burntmail.com>)
List pgsql-general
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.

All help is greatly appreciated!  Thanks,

Kevin

pgsql-general by date:

Previous
From: William Garrison
Date:
Subject: Do I have a corrupted database?
Next
From: "Richard Broersma"
Date:
Subject: PITR - base backup question