Percentage increase or decrease

I have a SQL script that displays the total amount purchased for the past 3 years I would like to also display the percent of increase or decrease year to year. Not sure how to go about that.
SELECT        Vendor_ID, Vendor_Name,
sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end) as Purchase_Ammount_2013,
sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end) as Purchase_Ammount_2014,
sum(case when ([Year]) = 2015 then Purchase_Ammount else 0 end) as Purchase_Ammount_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING        (Class_ID = 'MAN')

Open in new window

skull52Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
SELECT        Vendor_ID, Vendor_Name,
sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end) as Purchase_Ammount_2013,
sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end) as Purchase_Ammount_2014,
sum(case when ([Year]) = 2015 then Purchase_Ammount else 0 end) as Purchase_Ammount_2015,
sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end) * 100 /
nullif(sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end), 0) as Purchase_Percent_2014,
sum(case when ([Year]) = 2015 then Purchase_Ammount else 0 end) * 100 /
nullif(sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end), 0) as Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING        (Class_ID = 'MAN')
0
skull52Author Commented:
Thanks Scott, but I don't think it is calculating properly , it is not calculating the minus percentage, as you can see in the example below 2014  was $2,145 less than 2013 but it shows a positive 51.5 percent  and from 20014 to 2015 it shows a positive 122.5 percent increase but id is only a $513 dollar increase  Results
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The Class_ID in GROUP BY and HAVING is misplaced here, it should go into a WHERE only.
Since we are using SUMs multiple times, it is time for ad-hoc views (aka inline views, ...)
select Vendor_ID, Vendor_Name,
  Purchase_Amount_2013,
  Purchase_Amount_2014, (Purchase_Amount_2014-Purchase_Amount_2013)/Purchase_Amount_2013 as Percentage_2013,
  Purchase_Amount_2015, (Purchase_Amount_2013-Purchase_Amount_2015)/Purchase_Amount_2014 as Percentage_2014
from 
(
SELECT        Vendor_ID, Vendor_Name,
sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end) as Purchase_Ammount_2013,
sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end) as Purchase_Ammount_2014,
sum(case when ([Year]) = 2015 then Purchase_Ammount else 0 end) as Purchase_Ammount_2015
FROM            SSG_Vendor_Yearly_Totals
where Class_ID = 'MAN'
GROUP BY Vendor_ID, Vendor_Name
) data

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

skull52Author Commented:
Qlemo I am getting the following errors;

 Msg 207, Level 16, State 1, Line 2
Invalid column name 'Purchase_Amount_2013'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Purchase_Amount_2014'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Purchase_Amount_2014'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Purchase_Amount_2013'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Purchase_Amount_2013'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'Purchase_Amount_2015'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'Purchase_Amount_2013'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'Purchase_Amount_2015'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'Purchase_Amount_2014'.
0
Scott PletcherSenior DBACommented:
Sorry, you're right, I forgot the subtraction in the % calc:

(sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end) - sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end)) * 100 /
nullif(sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end), 0) as Purchase_Percent_2014,
(sum(case when ([Year]) = 2015 then Purchase_Ammount else 0 end) - sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end)) * 100 /
nullif(sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end), 0) as Purchase_Percent_2015
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
And you, skull52, had a type in your original question, and I took it over. And I forgot to make it percent instead of a float:
select Vendor_ID, Vendor_Name,
  Purchase_Amount_2013,
  Purchase_Amount_2014, (Purchase_Amount_2014-Purchase_Amount_2013)*100.0/Purchase_Amount_2013 as Percentage_2013,
  Purchase_Amount_2015, (Purchase_Amount_2013-Purchase_Amount_2015)*100.0/Purchase_Amount_2014 as Percentage_2014
from 
(
SELECT        Vendor_ID, Vendor_Name,
sum(case when ([Year]) = 2013 then Purchase_Ammount else 0 end) as Purchase_Amount_2013,
sum(case when ([Year]) = 2014 then Purchase_Ammount else 0 end) as Purchase_Amount_2014,
sum(case when ([Year]) = 2015 then Purchase_Ammount else 0 end) as Purchase_Amount_2015
FROM            SSG_Vendor_Yearly_Totals
where Class_ID = 'MAN'
GROUP BY Vendor_ID, Vendor_Name
) data

Open in new window

0
skull52Author Commented:
As always Scott you came through
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.