pivot on multiple column sql server -


i have below table , want use pivot on multiple column using sum aggregate.

category        station1         station2         station3 ------------------------------------------------------------- category1       5.26             6.25             7.28 category2       4.22             5.00             6.00 category3       5.00             4.00             8.00 category1       4.00             7.00             9.00 category2       2.00             5.00             8.00 

and want output like

my_station          category1           category2             category3 ------------------------------------------------------------------------ station1            sum_of_cat1         sum_of_cat2          sum_of_cat3 station2            sum_of_cat1         sum_of_cat2          sum_of_cat3 station3            sum_of_cat1         sum_of_cat2          sum_of_cat3 

with single query. not using loop

thanks

this isn't pivot. unpivot , repivot. i'm inclined write sql it:

select station mystation,        sum(case when category = 'category1' value else 0 end) category1,        sum(case when category = 'category2' value else 0 end) category2,        sum(case when category = 'category3' value else 0 end) category3 (select n.station, t.category,              (case when station = 'station1' station1                    when station = 'station2' station2                    when station = 'station3' station3               end) value       t cross join            (select 'station1' station union select 'station2' union select 'station3') n      ) t group station; 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -