Could anybody guide me how to pivot my data. I have:
RowID Dimension Value
1 Country Italy
1 Year 2011
1 GDP 4
1 Population 6
2 Country Spain
2 Year 2011
2 GDP 7
2 Population 5
I want in a such way:
RowID Country Year GDP Population
1 Italy 2011 4 6
2 Spain 2011 7 5
P.S. I use MS SQL Server 2008 R2 Express Edition. I tried to use PIVOT but it returned many rows with NULL so I could not figure out.
You can use
PIVOTfor this. This can be hard-coded if you know all of the values:See SQL Fiddle with Demo
Or of you do not have access to the
PIVOTfunction, then you can use an aggregate with aCASE:See SQL Fiddle with Demo
If you have an unknown number of values, then you can use dynamic sql:
See SQL Fiddle with Demo