Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-11-24 : 15:59:49
|
| Guys,I have to generate monthly payment schedule from the below informationNoofPayments TotalAmt ContractNum startdayofmonth4 4542 1 4Based on the above information the schedule should be generated starting 4th day of next month, payment amount rounded off to 2 digits after the decimalPymntDate PymntAmt12/4/08 1135.501/4/09 1135.502/4/09 1135.503/4/09 1135.5Any suggestions/inputs would helpThanks |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2008-11-24 : 17:26:03
|
If you create a table of numbers like this: CREATE TABLE numbers (n int not null primary key)INSERT numbers VALUES(1)INSERT numbers VALUES(2)INSERT numbers VALUES(3)INSERT numbers VALUES(4)... repeat as necessary The following should then work:SELECT DateAdd(month, DateDiff(month, 0, getdate()), Numbers.n) + StartDayOfMonth -1 PaymentDate, TotalAmt / NoOfPayments PaymentAmountFROM Numbers INNER JOIN YourTableWHERE Numbers.N <= NoOfPayments |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 23:59:57
|
| or use master..spt_values table. |
 |
|
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2008-11-25 : 17:19:29
|
quote: Originally posted by robvolk If you create a table of numbers like this: CREATE TABLE numbers (n int not null primary key)INSERT numbers VALUES(1)INSERT numbers VALUES(2)INSERT numbers VALUES(3)INSERT numbers VALUES(4)... repeat as necessary The following should then work:SELECT DateAdd(month, DateDiff(month, 0, getdate()), Numbers.n) + StartDayOfMonth -1 PaymentDate, TotalAmt / NoOfPayments PaymentAmountFROM Numbers INNER JOIN YourTableWHERE Numbers.N <= NoOfPayments
Thanks for the reply but when I runthe below query I get an error messageMsg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'WHERE'.SELECT DateAdd(month, DateDiff(month, 0, getdate()), Nums.id) + 4 -1 PaymentDate, Tot_Amt / Num_Pmt_s PaymentAmountFROM Nums INNER JOIN [contract]WHERE Nums.id <= Num_Pmt_sAny suggestions/inputs would helpThanks |
 |
|
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2008-11-25 : 17:28:00
|
quote: Originally posted by robvolk If you create a table of numbers like this: CREATE TABLE numbers (n int not null primary key)INSERT numbers VALUES(1)INSERT numbers VALUES(2)INSERT numbers VALUES(3)INSERT numbers VALUES(4)... repeat as necessary The following should then work:SELECT DateAdd(month, DateDiff(month, 0, getdate()), Numbers.n) + StartDayOfMonth -1 PaymentDate, TotalAmt / NoOfPayments PaymentAmountFROM Numbers INNER JOIN YourTableWHERE Numbers.N <= NoOfPayments
when I change the query to following it runs but does not give the desired resultSELECT DateAdd(month, DateDiff(month, 0, getdate()), Nums.id) + start_day_of_month -1 PaymentDate, [contract].Tot_Amt / [contract].Num_Pmt_s PaymentAmount, contrct_numFROM Nums INNER JOIN [contract] on nums.id = [contract].num_pmt_sWHERE Nums.id <= [contract].Num_Pmt_s and contrct_num = 200PymntDate PymntAmt ContrctNum2008-11-07 00:00:00.000 78.166666 200SELECT * FROM CONTRACT where contrct_num = 200num_pmt_s tot_amt contrct_num start_day_of_month3 234.50 200 4The result should be PymntDate PymntAmt ContrctNum2008-12-04 00:00:00.000 78.166666 2002009-01-04 00:00:00.000 78.166666 2002008-02-04 00:00:00.000 78.166666 200Any suggestions/inputs would help |
 |
|
|
|
|
|
|
|