SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Recursive date increment problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

MikeTimney
Starting Member

United Kingdom
1 Posts

Posted - 11/09/2012 :  08:49:29  Show Profile  Reply with Quote
I need to create a table (or view) that will create new rows based on an initial, and subsequent rows. I have a table fields called 'next replacement date' and ' lifecycle years'. For each record (in my case each component within a house) I need to work out when all subsequent replacement dates will occur based on the initial 'next replacement date' and the 'lifecycle years'

The initial calculation would be:

Next replacement date (from table)+ lifecycle years (from table)= Date 1

Then I would want to calculate subsequent dates like this:

Date 1 + lifecycle years = date 2
Date 2 + lifecycle years = date 3
Date 3 + lifecycle years = date 4

And so on, up to a limit of say, maximum date = 1/1/2050



I think this is what’s known as a recursive function (?) but I’m stumped as to how to do it

Can anyone help?


sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/09/2012 :  09:47:38  Show Profile  Reply with Quote
You can do it using recursive queries, but seems like it can also be done without recursion. If my understanding is correct, the formula would be the following, which is a straight query rather than recursive query.

Date1 = NextReplacementDate + Life cycle Years;
Date2 = NextReplacementDate + Life cycle Years * 2
Date3 = NextReplacementDate + Life cycle Years * 3

DateN = NextReplacementDate + Life cycle Years * N
Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
651 Posts

Posted - 11/11/2012 :  17:56:04  Show Profile  Reply with Quote
I'd just use a Tally Table for such a thing. If you'd post the CREATE TABLE and INSERT scripts to create an example table with some data, I could show you how easy it is.


--Jeff Moden
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000