sql - Pivot table -/+ aggregation -
i'm having trouble below query. need find percentage increase or decrease between years enrollment. reason i'm getting multiple line items , output incorrect. script runs fine produces incorrect results
script:
with cte ( select * ( select distinct dt.year , db.business_code , db.business_name , dt.time_key , fes.total_enrollment fact fes left outer join building db on fes.building_key = db.building_key left outer join mart.time dt on fes.time_key = dt.time_key ) source pivot ( sum(total_enrollment) [year] in ( [2012], [2011], [2010], [2009], [2008] ) ) pvt ) select company_code , company_name , [2012] , ( case when [2011] <> 0 convert(decimal(18, 2), ( [2012] - [2011] ) / convert(decimal(18, 2), ( [2011] ))) * 100 end ) '% degree -/+' , [2011] , ( case when [2010] <> 0 convert(decimal(18, 2), ( [2011] - [2010] ) / convert(decimal(18, 2), ( [2010] ))) * 100 end ) '% degree -/+' , [2010] , ( case when [2009] <> 0 convert(decimal(18, 2), ( [2010] - [2009] ) / convert(decimal(18, 2), ( [2009] ))) * 100 end ) '% degree -/+' , [2009] , ( case when [2008] <> 0 convert(decimal(18, 2), ( [2009] - [2008] ) / convert(decimal(18, 2), ( [2008] ))) * 100 end ) '% degree -/+' cte order company_name
heres sql output:
here desired output:
thank help...
Comments
Post a Comment