sql - pivot table and percentage comparison between columns -


i'm new using pivot tables sql , have been stuck on particular issue i'm hoping can help. script returns list of assignments , counts assignments each year. add column has percentage increase or decrease change previous year.

select * ( select [year_time], [assignment_code], [assignment_desc] raw_teacher ) source pivot (     count(assignment_code)     [year_time] in ([2012], [2011], [2010], [2009], [2008]) ) pvt 

current output:

enter image description here

desired output... enter image description here

you can calculation directly:

with cte (      select *           (      select [year_time], [assignment_code], [assignment_desc]      raw_teacher      ) source      pivot      (          count(assignment_code)          [year_time] in ([2012], [2011], [2010], [2009], [2008])      ) pvt     ) select assignment_desc, [2012], [2012]/[2011], [2011], [2011]/[2010],                         [2010], [2010]/[2009], [2009], [2009]/[2008], [2008] cte 

if values can 0, you'll want check that:

select asignment_desc,        [2012], (case when [2011] <> 0 [2012]/[2011] end),        [2011], (case when [2010] <> 0 [2011]/[2010] end),        . . . cte 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -