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 |
yonabout
Posting Yak Master
112 Posts |
Posted - 2008-12-17 : 10:18:11
|
Hi,I need to write a calculation that can take a value (at a date) and increase it by an annual percentage between that date and now.I can figure out how to do it using a cursor, but I'm going to need to do it in bulk, and I can see it taking a prohibitively long time.So say I have a figure of 1000 @ 01/01/2000.I then have a table of percentage increases that will look something like this:Year Increase2000 12001 12002 12003 12004 12005 12006 12007 12008 1So the value of my original 1000 (in 2000) will now be 1082.856 (worked out like this):2000 (1000 + 1% of 1000) = 10102001 (1010 + 1% of 1010) = 1020.1etc. etc2008 (1072.135 + 1% of 1072.135) = 1082.856So anyone got any ideas of the most optimised way I can do this in squizzle?Cheers,Yonabout |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-12-17 : 10:40:05
|
You can try something like this:DECLARE @Table TABLE ( CurrYear int ,Pct int ,Total float)DECLARE @id intDECLARE @Total floatSET @id = 1SET @total = 1000INSERT INTO @Table SELECT 2001, 1,0 UNION ALLSELECT 2002, 1,0 UNION ALLSELECT 2003, 1,0 UNION ALLSELECT 2004, 1,0 UNION ALLSELECT 2005, 1,0 UNION ALLSELECT 2006, 1 ,0 UNION ALLSELECT 2007 ,1,0 UNION ALLSELECT 2008 ,1,0UPDATE @TableSET @Total = Total = @Total*(1+pct*1.0/100) select * from @table |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-17 : 10:43:51
|
1082.856 only? Aren't you missing a year?DECLARE @Sample TABLE ( Year SMALLINT, Increase TINYINT )INSERT @SampleSELECT 2000, 1 UNION ALL -- 1010SELECT 2001, 1 UNION ALL -- 1020.1SELECT 2002, 1 UNION ALL -- 1030.301SELECT 2003, 1 UNION ALL -- 1040.60401SELECT 2004, 1 UNION ALL -- 1051.01005SELECT 2005, 1 UNION ALL -- 1061.520151SELECT 2006, 1 UNION ALL -- 1072.135353SELECT 2007, 1 UNION ALL -- 1082.856707SELECT 2008, 1 -- 1093.685274DECLARE @raise DECIMAL(38, 19)SET @Raise = 1000SELECT @Raise = @Raise * CAST(1.0E + increase / 100.0E AS DECIMAL(38, 19))FROM @SampleSELECT @Raise E 12°55'05.63"N 56°04'39.26" |
|
|
yonabout
Posting Yak Master
112 Posts |
Posted - 2008-12-17 : 10:53:15
|
Thanks both for your help - both solutions work greatCheers,Yonabout |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-12-17 : 10:53:36
|
Oops, I started at 2001 instead of 2000!Jim |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-12-17 : 11:12:58
|
Yonabout, I assume you are aware that none of these solutions will work if the increase is not identical from year to year, and then you are back to the cursor solution.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-17 : 11:37:57
|
Why do you say that?quote: Originally posted by blindman Yonabout, I assume you are aware that none of these solutions will work if the increase is not identical from year to year, and then you are back to the cursor solution.
If you only want get ending value there is no difference if you multiply1.1 * 1.4 * 1.2 * 1.3 or 1.1 * 1.2 * 1.3 * 1.4. You get same value.DECLARE @Sample TABLE ( Year SMALLINT PRIMARY KEY CLUSTERED, Increase DECIMAL(38, 19) )INSERT @SampleSELECT 2000, 1.2 UNION ALL -- 1012.000000SELECT 2001, 1.5 UNION ALL -- 1027.180000SELECT 2002, 1.8 UNION ALL -- 1045.669240SELECT 2003, 2.4 UNION ALL -- 1070.765302SELECT 2004, 3.1 UNION ALL -- 1103.959026SELECT 2005, 1.7 UNION ALL -- 1122.726330SELECT 2006, 4.3 UNION ALL -- 1171.003562SELECT 2007, 1.6 UNION ALL -- 1189.739619SELECT 2008, 1.1 -- 1202.826755SELECT 1000 * 1.012 * 1.015 * 1.018 * 1.024 * 1.031 * 1.017 * 1.043 * 1.016 * 1.011 UNION ALLSELECT 1000 * 1.043 * 1.012 * 1.024 * 1.015 * 1.018 * 1.031 * 1.017 * 1.011 * 1.016 E 12°55'05.63"N 56°04'39.26" |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-12-17 : 12:30:12
|
My bad. I was thinking about compounding, which would apply to investments but not to this salary scenario.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-12-17 : 14:24:57
|
As a single SELECT statement:DECLARE @Sample TABLE (Year SMALLINT, Increase TINYINT)INSERT @SampleSELECT 2000, 1 UNION ALL -- 1010SELECT 2001, 1 UNION ALL -- 1020.1SELECT 2002, 1 UNION ALL -- 1030.301SELECT 2003, 1 UNION ALL -- 1040.60401SELECT 2004, 1 UNION ALL -- 1051.01005SELECT 2005, 1 UNION ALL -- 1061.520151SELECT 2006, 1 UNION ALL -- 1072.135353SELECT 2007, 1 UNION ALL -- 1082.856707SELECT 2008, 1 -- 1093.685274--Single select statement:select 1000*exp(sum(log(1.0E + Increase/100.0E)))from @Sample ________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-12-17 : 17:10:29
|
[code]select [Year] = 1999+a.Number, [New Salary] = convert(numeric(10,2),round(power(1.01E,a.Number)*1000E,2))from F_TABLE_NUMBER_RANGE(1,9) aorder by 1Results:Year New Salary ----------- ------------ 2000 1010.002001 1020.102002 1030.302003 1040.602004 1051.012005 1061.522006 1072.142007 1082.862008 1093.69(9 row(s) affected)[/code]CODO ERGO SUM |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|