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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Statement - Percentage Change

Author  Topic 

miles away
Starting Member

1 Post

Posted - 2010-12-14 : 04:41:18
Hi there,

I'm hoping to get some assistance on an SQL statement that would calculate a year by year percentage change. The data is residing in the database in the format below;

Code year number
1 2005 20.2
2 2006 25
3 2007 38.6
4 2008 61.8

I would like the statement to calculate the year by year percentage change similar to the example below;

Year 2005 2006 2007 2008
number 20.2 25 38.6 61.8
% Change 23.76% 54.40% 60.10%

Any suggestions would be greatly appreciated

Devart
Posting Yak Master

102 Posts

Posted - 2010-12-14 : 05:40:21
Hello,

For example:

SELECT
t1.year,
t1.number,
cast(100*(t1.number-t2.number)/t2.number as decimal(18,2)) AS change
FROM
<your_table_name> t1 LEFT JOIN <your_table_name> t2 ON t1.year=t2.year+1

Devart,
SQL Server Tools:
dbForge Schema Compare
dbForge Data Compare
dbForge Query Builder
Go to Top of Page
   

- Advertisement -