I have an sql query “Select * from tablename” whose output is
col1 col2
A 1
B 2
C 3
I want to modify the above output to below as following
A B C
1 2 3
Please let me know how could I achieve this
You will need to perform a
PIVOT. There are two ways to do this with PIVOT, either a Static Pivot where you code the columns to transform or a Dynamic Pivot which determines the columns at execution.Static Pivot:
See SQL Fiddle with Demo
Dynamic Pivot:
See SQL Fiddle with Demo
If you do not want to use the
PIVOTfunction, then you can perform a similar type of query withCASEstatements:See SQL Fiddle with Demo