?
Solved

Creating a Payment Schedule Table

Posted on 2014-02-12
2
Medium Priority
?
238 Views
Last Modified: 2014-02-17
I would like to be able to create a table of scheduled payments based off data in another table.  The fields I would have available would be:  Payment Start Date, # of Months, and Payment Amount.  So for example, if I had the following:

StartDate     # of Months             Amount
03/01/14               36                     $250.00

I would want to create  table that returns 36 rows with the start date being incremented for each month.  I was thinking the ROW over command would work to increment the start date, but I'm not sure how to create the number of rows off the value of the field # of Months.
0
Comment
Question by:garberj76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 1500 total points
ID: 39854485
try the below code

declare @T TABLE
(
 Number INT
)

;WITH C AS
(
  SELECT 0 AS T
  UNION ALL
  SELECT T+1 FROM C WHERE T< 99
)
INSERT INTO @T
SELECT T FROM C

DECLARE @CurrentTable TABLE
(
	StartDate	DATE
	,NumberOfMonths INT
	,Amount Money
)

INSERT INTO @CurrentTable VALUES('03/01/14',36,250)
select * from @CurrentTable

-- if you want the date to be incremented by month
SELECT DATEADD(mm,number,StartDate) as IncrementedDate,Amount FROM @CurrentTable C
CROSS JOIN @T T
WHERE T.Number < C.NumberOfMonths

-- if you want the date to be incremented by Days
SELECT DATEADD(DD,number,StartDate) as IncrementedDate,Amount FROM @CurrentTable C
CROSS JOIN @T T
WHERE T.Number < C.NumberOfMonths

Open in new window

0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 39857673
I wrote a script to return a row for every day between two dates - in this case you'd change it to increment months, but it's the same concept:

http://trycatchfinally.net/2012/03/return-a-list-of-all-dates-between-a-start-and-end-date/

In your case, the months version would be:

DECLARE @StartDate DATETIME,
        @Months INT
     
    SET @StartDate = '2014-03-01'
    SET @Months   = 36
     
;WITH numberlist(number)
   AS (SELECT RANK() over(order by c1.object_id,
                                   c1.column_id,
                                   c2.object_id,
                                   c2.column_id)
         from sys.columns c1
        cross
         join sys.columns c2)
SELECT DATEADD(mm, number-1, @StartDate)
  FROM numberlist
 WHERE number <= @Months

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question