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:
desired output...
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
Post a Comment