You are on page 1of 4

Using PIVOT and UNPIVOT Clause

- The PIVOT clause is new for oracle Database 11g and enable you to
rotate rows into columns in the output from an query, and, at the same
time, to run an aggregation function on the data.
- Oracle 11g also has an UNPIVIOT clause that rotates column into
rows in the output from an query
PIVOT
C Php
SELECT *
FROM
(
I nner Query
)
PIVOT ( aggregate_function FOR pivot_column IN ( list_of_values)
)
ORDER BY . ;
Yu cu:
- Show the total sales amount of product type #1, #2 and #3 for the first
fourth months in 2000


Pivot trn 1 column
V d 1:
select *
from
(
select month, prod_id, amount
from all_sales
where year = 2000 and prod_id in (1,2,3)
)
pivot ( sum(amount) for month in (1 as Jan, 2 as Feb, 3 as Mar,
4 as Apr))
Ch :
- There is an inner and outer query. The inner query get the month,
product id, and amount from all_sales table and pass the result to the
outer query
- Sum(amount) For month in (1 as Jan, 2 as Feb, 3 as Mar, 4 as Apr) is
the line in the PIVOT clause
o The Sum() function adds up the sales amounts for the product
types in the first four months (the month are list in the IN part)
o The month column from all_sales table is used as the pivot
column. This mean that the months appear as columns in the
output

PIVOT nhiu column
You can pivot on multiple columns by placing those columns in the FOR
part of the PIVOT.

V d 1:
select *
from ( select prod_id, month, amount
from all_sales
where year = 2000 and prod_id in (1,2,3)
)
pivot ( sum(amount) for (month, prod_id) in ( (1,2) Jan_Prod2,
(2,3) Feb_Prod3,
(3,1) Mar_Prod1,
(4,2) Apr_Prod2 ) )
V d 2:

select *
from ( select prod_id, month, amount
from all_sales
where year = 2000 and prod_id in (1,2,3)
)
pivot ( sum(amount) for (month, prod_id) in ( (1,1) Jan_Prod1,
(2,2) Feb_Prod2,
(3,3) Mar_Prod3,
(4,1) Apr_Prod1 ) )

Ch :
- The cells in the output show the sum of the sales amount for each
product type in the specified month ( the product type and month to
query are place in the list of values in the IN part.
Using Multiple Aggregate Functions in a Pivot
- You can use multiple aggregate functions in a pivot. For example, the
following query uses Sum() to get the total sales for the product types
in January and February and AVG() to get the average of the sales
V d 1:
select *
from ( select prod_id, month, amount
from all_sales
where year = 2000 and prod_id in (1,2,3)
)
pivot (sum(amount) as sum_amount , avg(amount) avg_amount
for (month) in (1 as Jan, 2 Feb) )
UNPIVOT CLAUSE
- The UNPIVOT clause rotates column into rows
- The example in this section use the following table named
pivot_sales_data ( create by the store_schema.sql script)
- Pivot_sales_data is populated by a query that returns a pivoted version
of the sales data:
V d 1:
To table
create table pivot_sales_date
as
select *
from (
select month, prod_id, amount
from all_sales
where year = 2000 and prod_id in (1,2,3)
)
pivot (sum(amount) for month in (1 as Jan, 2 as Feb, 3 as Mar, 4
as Apr) )
order by prod_id ;

Xem d liu
select * from pivot_sales_date;
Unpivot

select *
from pivot_sales_date
unpivot ( amount for month in (Jan, Feb, Mar, Apr) )
order by prod_id

You might also like