I’m new to SQL and I’m wondering how to pivot a table like:
Col1 Col2 Col3
1 a w
2 a x
1 b y
2 b z
Into
Col1 a b
1 w y
2 x z
I was playing with GROUP BY but I can’t seem to be able to turn unique rows into columns
This can be done using an aggregate function with a
CASEexpression:See SQL Fiddle with Demo
If you are using an RDBMS with a
PIVOTfunction (SQL Server 2005+ / Oracle 11g+), then your query would be similar to this (Note: Oracle syntax below):See SQL Fiddle with Demo
The last way that you can do this is by using multiple joins on the same table:
See SQL Fiddle with Demo
All give the result: