Difference between revisions of "Multiply sets"

From SQLZOO
Jump to: navigation, search
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
Multiply across a result set.
+
<p>Multiplying across a result set allows for interest rates to calculated correctly.</p>
 +
<p>In this example we get the interest after 4 years imagine over the 4 years we have rates 5%, 4%, 5% and 3% adding these rates
 +
to get 17% (£117) isn't correct.</p> <p>To get the correct results you have to follow the steps given here.</p>
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 12: Line 14:
 
</source>
 
</source>
 
<div>
 
<div>
<p>In this example we are attempting to see how much money has been obtained over 4
 
years due to interest.</p>
 
<p>As the added up rate is not accurate 5 + 4 + 5 + 3 = 17%</p>
 
 
<p>We need to instead find the logarithm of the compound interest and then we need to sum that.</p>
 
<p>We need to instead find the logarithm of the compound interest and then we need to sum that.</p>
 
<p>SELECT SUM(LN((rate/100)+1)) FROM interest</p>
 
<p>SELECT SUM(LN((rate/100)+1)) FROM interest</p>
 
<p>Then we inverse or take the exponent of the logarithm with</p>
 
<p>Then we inverse or take the exponent of the logarithm with</p>
 
<p>SELECT EXP(SUM(LN((rate/100)+1))) FROM interest</p>
 
<p>SELECT EXP(SUM(LN((rate/100)+1))) FROM interest</p>
<p>and then finally to get the amount after 4 years we times this amount by 100 ($100).</p>
+
<p>and then finally to get the amount after 4 years we times this amount by 100 (£100).</p>
 
</div>
 
</div>
<source lang='sql' class='def'>SELECT EXP(SUM(LN((rate/100)+1)))*100
+
<source lang='sql' class='def'>
FROM interest</source>
+
SELECT EXP(SUM(LN((rate/100)+1)))*100
<div class="ecomm e-mysql" style="display: none"></div>
+
  FROM interest</source>
 +
<div class="ecomm e-sqlserver" style="display: none"></div>
 
</div>
 
</div>
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 15:02, 2 August 2012

Multiplying across a result set allows for interest rates to calculated correctly.

In this example we get the interest after 4 years imagine over the 4 years we have rates 5%, 4%, 5% and 3% adding these rates to get 17% (£117) isn't correct.

To get the correct results you have to follow the steps given here.

schema:scott
DROP TABLE interest
 CREATE TABLE interest(
  yr INTEGER,
  rate INTEGER );
INSERT INTO interest VALUES (2002,5);
INSERT INTO interest VALUES (2003,4);
INSERT INTO interest VALUES (2004,5);
INSERT INTO interest VALUES (2005,3);

We need to instead find the logarithm of the compound interest and then we need to sum that.

SELECT SUM(LN((rate/100)+1)) FROM interest

Then we inverse or take the exponent of the logarithm with

SELECT EXP(SUM(LN((rate/100)+1))) FROM interest

and then finally to get the amount after 4 years we times this amount by 100 (£100).

SELECT EXP(SUM(LN((rate/100)+1)))*100
  FROM interest
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense