I have a schema like this
demo(id, val, month, year, decide)
Demo data and schema is given in this fiddle http://sqlfiddle.com/#!3/dd89d5/1
In output i want to transform the rows to columns. In the output i want,
ID (11 14) (12 14) (2 15) ... decider
101 0.45 0.5 0.3 411
102 0.4 0.2 0.1 411
I want the month, year to be sorted
I went through lot of threads and found a fiddle. And tried to customize to suit my need
Here is what i tried http://sqlfiddle.com/#!3/dd89d5/1
But could not get the solution. Please help me
Here is your table
Use
Dense_Rank
to order the column for pivotSelect the columns for pivot and declare a variable to replace
NULL
with zeroNow pivot it
Result
Here is the Fiddle http://sqlfiddle.com/#!3/95111/1 (If any error occur on load press RUNSQL)