How to Convert Rows to Columns: PIVOT Function

To convert rows to columns in Oracle SQL, you can use the PIVOT operator. Here’s an example:

SQL PIVOT Operator

Let’s say you have a table named “sales” with the following data:

And you want to convert the rows into columns, with each product as a separate column and the months as the rows. You can achieve this using the PIVOT operator as follows:

This query will produce the following output:

In this output, each row represents a month, and each column represents a product. The values in each cell represent the revenue for that product in that month.


Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.