sql server - T-SQL using how to use PIVOT function -


i have following table structure in sql (using t-sql): sqlfiddle: http://sqlfiddle.com/#!6/e5edc/1/0

the data this:

enter image description here

now transpose structure following:

  • columns [01_amount] [12_amount] , columns [01_active] [12_active] as rows instead of columns
  • all rows of [initials] separate columns

should this:

enter image description here

how go this? pivot function looks rather complicated i'm new sql. can me in right direction? :-)

ok need first unpivot data, done in cte. need pivot again:

;with cte as(select initials, v, col main unpivot(v col in([01_amount], [02_amount])) u) select * cte pivot(max(v) initials in([rw],[nb]))p 

in unpivot part add 24 column names amounts , active bits. in pivot part add possible values initials.

but if don't want manually list possible values initials need make dynamic query unpivoting , pivoting.

here demo 2 columns , expand http://sqlfiddle.com/#!6/4cf36/2


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -