Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Calculating interest query Expand / Collapse
Author
Message
Posted Thursday, February 25, 2010 6:15 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 2, 2014 11:57 AM
Points: 71, Visits: 172
I'm currently in the process of trying to calculate interest we need to pay out to our customers. I have calculated refunds for all of our customers that returns the data in the following format:

acct_no , refund, month, year

I have another table that contains interest rate information for each month that has the following format:

interest rate, month, year

I need a way to calculate the compound interest and so far I am having no luck. Thank you in advance for any help.
Post #873124
Posted Thursday, February 25, 2010 10:51 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:42 AM
Points: 36,727, Visits: 31,178
Nathan,

My recommendation would be for you to read the article at the first link in my signature line below. Following the recommendations for how to post in that article will probably get you a fully tested answer in the form of code and in very short order. Help us help you.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #873178
Posted Friday, February 26, 2010 2:10 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 3:21 AM
Points: 73, Visits: 347
as already mentioned, pls provide create table, insert data and expected results.
Post #873257
Posted Friday, February 26, 2010 11:42 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 2, 2014 11:57 AM
Points: 71, Visits: 172
Thank you for pointing out proper posting etiquette. I had not seen that article before and I apologize.

IF OBJECT_ID('TempDB..#InterestRates','U') IS NOT NULL
DROP TABLE #InterestRates

CREATE TABLE #InterestRates
(
[InterestRate] [float] NULL,
[Month] [int] NULL,
[year] [int] NULL
)

INSERT INTO #InterestRates
(InterestRate,[Month],[Year])
SELECT '0.105','8','2007' UNION ALL
SELECT '0.105','9','2007' UNION ALL
SELECT '0.105','10','2007' UNION ALL
SELECT '0.105','11','2007' UNION ALL
SELECT '0.105','12','2007' UNION ALL
SELECT '0.105','1','2008' UNION ALL
SELECT '0.105','2','2008' UNION ALL
SELECT '0.105','3','2008' UNION ALL
SELECT '0.105','4','2008' UNION ALL
SELECT '0.105','5','2008' UNION ALL
SELECT '0.105','6','2008' UNION ALL
SELECT '0.105','7','2008' UNION ALL
SELECT '0.105','8','2008' UNION ALL
SELECT '0.105','9','2008' UNION ALL
SELECT '0.105','10','2008' UNION ALL
SELECT '0.105','11','2008' UNION ALL
SELECT '0.105','12','2008' UNION ALL
SELECT '0.105','1','2009' UNION ALL
SELECT '0.105','2','2009' UNION ALL
SELECT '0.105','3','2009' UNION ALL
SELECT '0.105','4','2009' UNION ALL
SELECT '0.105','5','2009' UNION ALL
SELECT '0.105','6','2009' UNION ALL
SELECT '0.0067','7','2009' UNION ALL
SELECT '0.0067','8','2009' UNION ALL
SELECT '0.0067','9','2009' UNION ALL
SELECT '0.0055','10','2009' UNION ALL
SELECT '0.0055','11','2009' UNION ALL
SELECT '0.0055','12','2009' UNION ALL
SELECT '0.0055','1','2010' UNION ALL
SELECT '0.0055','2','2010'

IF OBJECT_ID('TempDB..#Refunds','U') IS NOT NULL
DROP TABLE #Refunds

CREATE TABLE #Refunds
(
[Acct_no] [varchar(20)] NULL,
[Amount] [float] NULL,
[Month] [int] NULL,
[year] [int] NULL

)

INSERT INTO #Refunds
(Acct_no,Amount, [Month],[year])
SELECT '1271003600','333.107456586203','1','2008' UNION ALL
SELECT '1271003600','87.6816131178288','1','2009' UNION ALL
SELECT '1271003600','103.602002310821','2','2008' UNION ALL
SELECT '1271003600','81.9722910242125','2','2009' UNION ALL
SELECT '1271003600','72.3401647559977','3','2008' UNION ALL
SELECT '1271003600','86.1625895742832','3','2009' UNION ALL
SELECT '1271003600','103.361218989159','4','2008' UNION ALL
SELECT '1271003600','84.6065155612006','4','2009' UNION ALL
SELECT '1271003600','92.2834655375568','5','2008' UNION ALL
SELECT '1271003600','84.6688365382596','5','2009' UNION ALL
SELECT '1271003600','89.0687598341003','6','2008' UNION ALL
SELECT '1271003600','79.1241550533365','6','2009' UNION ALL
SELECT '1271003600','123.131877952709','7','2008' UNION ALL
SELECT '1271003600','89.3938505146093','7','2009' UNION ALL
SELECT '1271003600','87.2510378856992','8','2007' UNION ALL
SELECT '1271003600','94.0687846832911','8','2008' UNION ALL
SELECT '1271003600','340.381691069552','8','2009' UNION ALL
SELECT '1271003600','80.3632310400272','9','2007' UNION ALL
SELECT '1271003600','121.96840107118','9','2008' UNION ALL
SELECT '1271003600','87.1037670414245','10','2007' UNION ALL
SELECT '1271003600','94.9861134399298','10','2008' UNION ALL
SELECT '1271003600','73.8008470324637','11','2007' UNION ALL
SELECT '1271003600','78.537373182859','11','2008' UNION ALL
SELECT '1271003600','90.7988754111144','12','2007' UNION ALL
SELECT '1271003600','88.4954811818565','12','2008' UNION ALL
SELECT '1271005700 ','651.764','1','2008' UNION ALL
SELECT '1271005700 ','650.766','1','2009' UNION ALL
SELECT '1271005700 ','897.994','2','2008' UNION ALL
SELECT '1271005700 ','688.486','2','2009' UNION ALL
SELECT '1271005700 ','721.466','3','2008' UNION ALL
SELECT '1271005700 ','718.654','3','2009' UNION ALL
SELECT '1271005700 ','913.072','4','2008' UNION ALL
SELECT '1271005700 ','863.224','4','2009' UNION ALL
SELECT '1271005700 ','735.081','5','2008' UNION ALL
SELECT '1271005700 ','647.368','5','2009' UNION ALL
SELECT '1271005700 ','607.721','6','2008' UNION ALL
SELECT '1271005700 ','580.359','6','2009' UNION ALL
SELECT '1271005700 ','718.969','7','2008' UNION ALL
SELECT '1271005700 ','693.378','7','2009' UNION ALL
SELECT '1271005700 ','734.005','8','2007' UNION ALL
SELECT '1271005700 ','591.788','8','2008' UNION ALL
SELECT '1271005700 ','679.773','8','2009' UNION ALL
SELECT '1271005700 ','560.296','9','2007' UNION ALL
SELECT '1271005700 ','685.047','9','2008' UNION ALL
SELECT '1271005700 ','682.834','10','2007' UNION ALL
SELECT '1271005700 ','608.706','10','2008' UNION ALL
SELECT '1271005700 ','586.172','11','2007' UNION ALL
SELECT '1271005700 ','650.191','11','2008' UNION ALL
SELECT '1271005700 ','858.672','12','2007' UNION ALL
SELECT '1271005700 ','711.708','12','2008'

The formula I am using to calculate interest is
Interest(month) =( Refund(PreviousMonth) + Interest(Previous Month) ) * (1+ InterestRate(Month)/12)

For Example
Interest(August 2008) = ( 0 + 0)*(1+.105/12) = 0
Interest(September 2008) = ( 87.25 + 0 )*(1+.105/12) = 88.01
Interest(October 2008) = (80.36 + 88.01)*(1+.105/12) = 169.851

There are no more refunds to add after August 2009 but we continue to accrue interest until February 2010

For the account above I get a Final balance of 3011.41

If anything above is unclear please let me know and thank you for your help.
Post #873628
Posted Friday, February 26, 2010 2:51 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 10:45 AM
Points: 1,072, Visits: 6,339
Hi Nathan

Thanks for posting the sample data and expected results, it doesn't half make a difference. Top work.

Here you go.

;WITH CTEdata AS (
SELECT ExecSeq = ROW_NUMBER() OVER (ORDER BY Dates.[year], Dates.[Month]),
r.Acct_no, r.Amount, Dates.[Month], Dates.[year], i.InterestRate, CAST(0 AS float) AS Interest
FROM (
SELECT [Year], [month]
FROM (
(SELECT TOP 12 [month] = ROW_NUMBER() OVER(ORDER BY [name]) FROM master.sys.columns) m
CROSS JOIN
(SELECT 2007 AS [Year] UNION ALL SELECT 2008 UNION ALL SELECT 2009 UNION ALL SELECT 2010) y
)
WHERE NOT ([Year] = 2010 AND [month] > 2) AND NOT ([Year] = 2007 AND [month] < 8)
) Dates
LEFT JOIN #Refunds r ON r.[year] = Dates.[year] AND r.[Month] = Dates.[Month]
LEFT JOIN #InterestRates i ON i.[year] = Dates.[year] AND i.[Month] = Dates.[Month]),

Calculator AS (
SELECT ExecSeq,
Acct_no,
Amount,
[Month],
[year],
InterestRate,
Interest
FROM CTEdata WHERE ExecSeq = 1
UNION ALL
SELECT cr.ExecSeq,
ISNULL(cr.Acct_no, lr.Acct_no),
ISNULL(cr.Amount, 0),
cr.[Month],
cr.[year],
cr.InterestRate,
Interest = (lr.Amount + lr.Interest) * (1 + (cr.InterestRate/12))
FROM Calculator lr
INNER JOIN CTEdata cr ON cr.ExecSeq = lr.ExecSeq+1
)
SELECT * FROM Calculator ORDER BY ExecSeq




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #873737
Posted Friday, February 26, 2010 3:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:42 AM
Points: 36,727, Visits: 31,178
nathan 7372 (2/26/2010)
Thank you for pointing out proper posting etiquette. I had not seen that article before and I apologize.


Absolutely no problem and no need to apologize. You're new and I figured you needed a leg up to get a good answer. Looks like Chris jumped right in there as soon as you posted the data. Very well done.

If I have time tonight after I get home from work, I'll try to show you a different way.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #873741
Posted Friday, February 26, 2010 3:34 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 10:45 AM
Points: 1,072, Visits: 6,339
Jeff Moden (2/26/2010)
nathan 7372 (2/26/2010)
Thank you for pointing out proper posting etiquette. I had not seen that article before and I apologize.


Absolutely no problem and no need to apologize. You're new and I figured you needed a leg up to get a good answer. Looks like Chris jumped right in there as soon as you posted the data. Very well done.

If I have time tonight after I get home from work, I'll try to show you a different way.


Absolutely. Excellent description of the problem, easily-consumable data - what more could you ask for?

Jeff, apologies if you feel I was treading on your toes here - I figured you'd come up with a quirky update solution, so the OP will have choice. The obvious third alternative (correlated subquery/triangular join) will likely appear too - they're all yours, mate

Cheers

ChrisM



Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #873744
Posted Friday, February 26, 2010 3:38 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:42 AM
Points: 36,727, Visits: 31,178
ChrisM@home (2/26/2010)
Jeff, apologies if you feel I was treading on your toes here - I figured you'd come up with a quirky update solution, so the OP will have choice. The obvious third alternative (correlated subquery/triangular join) will likely appear too - they're all yours, mate


Oh no... please don't feel that way. Absolutely no offense taken over here and no apologies required. You did great, Chris. In fact, I don't feel pressured now because someone did have the time to produce a coded answer to some great data.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #873747
Posted Friday, February 26, 2010 4:26 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
Besides the method of calculating and updating, you'll want to reconsider the datatype choices. Generally, FLOAT is inappropriate for financial calculations due to rounding issues. Microsoft recommends DECIMAL instead.

See http://msdn.microsoft.com/en-us/library/system.decimal.aspx

To quote the "remarks" in that article:
The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding.
Post #873754
Posted Friday, February 26, 2010 5:46 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 2, 2014 11:57 AM
Points: 71, Visits: 172
Thank you very much for your help. I only have a very basic understanding of CTEs but I'm sure I will be able to puzzle out exactly what's happening after a bit of studying. I'm sure it's much better than the only solution I was able to imagine which involved a cursor and some temporary tables. I will let you know how it works once it finishes running. I had to raise the recursion limit and the query has been running for over an hour so far. Thankfully it only has to be run once assuming the numbers come out right.

I look forward to seeing some other solutions if anyone has any. I always like to learn more and this problem really stumped me.
Post #873768
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse