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ú Pháp
SELECT *
FROM
(
I nner Query
)
PIVOT ( aggregate_function FOR pivot_column IN ( list_of_values)
)
ORDER BY …. ;
Yêu cầu:
- Show the total sales amount of product type #1, #2 and #3 for the first
fourth months in 2000


Pivot trên 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 nhiều 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:
Tạo 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ữ liệu
select * from pivot_sales_date;
Unpivot

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


Sign up to vote on this title
UsefulNot useful