Thursday, August 8, 2013

Oracle SQL - Rows to Coulmn conversion

Pivoting techniques (Rows to Column conversion) is very possible in Oracle SQL. There are two methods used popularly for Pivoting in Oracle SQLO( From Oracle 8i) . They are:

1. Using Decode ( or CASE )
2. Using Sys_connect_by_path


1. Using Decode 

This method uses Decode and group by together. 




2. Using Sys_connect_by_path 

As Sys_connect_by_path is used along with hierarchical queries, we need to make any query in that format for doing the pivot . It returns the path of a column value from root to node, with column values separated by char for each row returned by CONNECT BY condition.


Though Decode gives more flexibility, Sys_connect_by_path is recommended in case row values are unknown or not fixed.