Pivot and Unpivot Operators in Oracle Database 11g1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

11/29/2018 Pivot and Unpivot Operators in Oracle Database 11g

Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop

Search... Search
Pivot and Unpivot Operators in Oracle Database 11g
Pivot:
Popular Posts

Informatica Scenario Based Interview Questions with


Answers - Part 1
The pviot operator converts row data to column data and also can do aggregates while
converting. To see how pivot operator works, consider the following "sales" table as any example
Unix Sed Command to Delete Lines in File - 15 Examples

Table Name: Sales String Functions in Hive

Top Examples of Awk Command in Unix


customer_id product price
-------------------------------------- Sed Command in Unix and Linux Examples
1 A 10
1 B 20 Date Functions in Hive

2 A 30
SQL Queries Interview Questions - Oracle Part 1
2 B 40
2 C 50 Design/Implement/Create SCD Type 2 Effective Date
3 A 60 Mapping in Informatica
3 B 70
Top Unix Interview Questions - Part 1
3 C 80
Grep Command in Unix and Linux Examples
The rows of the "sales" table needs to be converted into columns as shown below

Table Name: sales_rev Have Questions? Follow Me

cutomer_id a_product b_product c_product


-----------------------------------------
1 10 20
2 30 40 50
3 60 70 80
https://www.folkstalk.com/2011/12/pivot-and-unpivot-operators-in-oracle.html 1/4
11/29/2018 Pivot and Unpivot Operators in Oracle Database 11g

vijay bhaskar
Add to circles
The query for converting the rows to columns is

SELECT *
FROM (SELECT customer_id,product,price from sales)
pivot ( sum(price) as total_price for (product) IN ( 'A' as a, 'B' as b, 'C'

Pivot can be used to generate the data in xml format. The query for generating the data into xml
fomat is shown below.

SELECT *
FROM (SELECT customer_id,product,price from sales)
pivot XML ( sum(price) as total_price for (product) IN ( SELECT distinct prod
994 have me in circles View all

If you are not using oracle 11g database, then you can implement the unpivot feature as
converting rows to columns

Unpivot:

Unpivot operator converts the columns into rows.


Table Name: sales_rev

cutomer_id a_product b_product c_product


-----------------------------------------
1 10 20
2 30 40 50
3 60 70 80

Table Name: sales

https://www.folkstalk.com/2011/12/pivot-and-unpivot-operators-in-oracle.html 2/4
11/29/2018 Pivot and Unpivot Operators in Oracle Database 11g

customer_id product price


---------------------------
1 A 10
1 B 20
2 A 30
2 B 40
2 C 50
3 A 60
3 B 70
3 C 80

The query to convert rows into columns is

SELECT *
FROM sales_rev
UNPIVOT [EXCLUDE NULLs | INCLUDE NULLs] (price FOR product IN (a_product AS '

Points to note about the query


The columns price and product in the unpivot clause are required and these names
need not to be present in the table.
The unpivoted columns must be specified in the IN clause
By default the query excludes null values.
If you like this post, then please share it by clicking on the +1 button.

2 comments:

Neel 30 January, 2012 22:04


Any idea on how can we achieve pivot n unpivot without using these operator. i.e by using simple
sql how can we achieve it (using same above example).
Thanks,
Neel

https://www.folkstalk.com/2011/12/pivot-and-unpivot-operators-in-oracle.html 3/4
11/29/2018 Pivot and Unpivot Operators in Oracle Database 11g

Reply

Replies

vijay bhaskar 31 January, 2012 03:47


You can find the pivot operation at http://www.folkstalk.com/2010/04/converting-rows-
to-columns.html

And for unpivot, a simple query is using union all.

Reply

Enter your comment...

Comment as: Google Accoun

Publish Preview

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

https://www.folkstalk.com/2011/12/pivot-and-unpivot-operators-in-oracle.html 4/4

You might also like