Difference between revisions of "Multiply sets"

From SQLZOO
Jump to: navigation, search
(Created page with "Multiply across a result set. <div class='ht'> <div class=params>schema:scott</div> <source lang=sql class='tidy'>DROP TABLE interest</source> <source lang=sql class='setup'> ...")
 
Line 5: Line 5:
 
<source lang=sql class='setup'> CREATE TABLE interest(
 
<source lang=sql class='setup'> CREATE TABLE interest(
 
   yr INTEGER,
 
   yr INTEGER,
   rate DECIMAL );
+
   rate INTEGER );
 
INSERT INTO interest VALUES (2002,0.05);
 
INSERT INTO interest VALUES (2002,0.05);
 
INSERT INTO interest VALUES (2003,0.04);
 
INSERT INTO interest VALUES (2003,0.04);

Revision as of 11:19, 30 July 2012

Multiply across a result set.

schema:scott
DROP TABLE interest
 CREATE TABLE interest(
  yr INTEGER,
  rate INTEGER );
INSERT INTO interest VALUES (2002,0.05);
INSERT INTO interest VALUES (2003,0.04);
INSERT INTO interest VALUES (2004,0.05);
INSERT INTO interest VALUES (2005,0.03);

In this example we are attempting to see how much money has been obtained over 4 years due to interest. As the added up rate is not accurate 5 + 4 + 5 + 3 = 17% We need to instead find the logarithm of the compound interest and then we need to sum that. SELECT SUM(LN(1+rate/100)) FROM interest Then we inverse or take the exponent of the logarithm with SELECT EXP(SUM(LN(1+rate/100))) FROM interest and then finally to get the amount after 4 years we times this amount by 100 ($100).

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

Variants
Actions
Reference
Toolbox
Google AdSense