|
spendyala
Starting Member
15 Posts |
Posted - 2009-01-15 : 18:33:49
|
| Hi, I have a funding table which is as follows. I have an account no, funddate and amount columns in the table. I have to take the amount on the minimum date for this account and calculate the rest of months amount to be paid by the student. For this i am using a CTE and the CTE is going into a recursive loop, even though i am getting the correct amount for the first fewones. Please let me know where i am going wrong. In the below example, i have funding amount of 2635 for the month of may, so i have to calculate the amount for the months of june - november. In november, i have to add the existing amount for the month of november in the funding table to the calculation, i am doing. Pls let me know if you have any questions. declare @funding table(accountno int, funddate datetime, lstartdate datetime, lenddate datetime, amount int)insert @funding select 1, '2008-05-17','2008-04-11','2008-11-20',2635union all select 1, '2008-11-04','2008-04-11','2008-11-20',300--union all select 2,'2008-03-01','2008-04-10','2008-12-21',3000declare @minmonth int select @minmonth = min(datepart(month,funddate)) from @funding;WITH Yak (accountno,m, amount , discount,dated)AS(select accountno,datepart(month,funddate),amount,0,0 from @funding where datepart(month,funddate) = @minmonthunion allselect funding.accountno, m+1, yak.amount - (yak.amount * 30 /datediff(dd,lstartdate,lenddate)) ,0,datediff(dd,lstartdate,lenddate)from @funding funding join yak on yak.accountno = funding.accountnowhere yak.amount > 1000)select * from yakgroup by accountno,m,amount,discount,datedthank you,Sri. |
|