How To Use The PIVOT CLAUSE In Oracle
Description
Pivot Clause allows you to write a cross-tabulation query in Oracle. Pivot clause takes the data in separate row, aggregates it and converts it into column.
Syntax
The syntax for the Pivot clause in Oracle is given below-
- Select
- *
- from
- (
- select
- column1,
- column2…column n
- from
- tables
- where
- conditions
- ) PIVOT (
- aggregate_function(column 2) for column 2 IN(expr1, expr2, ….Expr_n) | subquery
- )
- ORDER BY
- expression [ASC | DESC];
NOTE: Pivot clause can be used in the following versions in Oracle-
Oracle 11g, Oracle 12c.
Example
This example shows, how to use the Pivot clause in Oracle.
- Create table customer (
- customer_id number(25),
- customer_name vachar2(50),
- product_code varchar2(5),
- quantity number(50),
- constraint customer_pk primary key(customer_id)
- );
Inserting values
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (101, 'SMITH', 'A1', 10);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (102, 'JONES', 'B2', 20);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (103, 'ANDERSON', 'C3', 30);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (104, 'JONES', 'B4', 40);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (105, 'ANDERSON', 'K4', 20);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (106, 'JONES', 'C4', 40);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (107, 'SMITH', 'B3', 20);
- Insert into customer(
- customer_id, customer_name, product_code,
- quantity
- )
- values
- (108, 'JONES', 'D1', 10);
Now, you will select the records from the order table with select statement.
Syntax
- select
- column1,
- column2,
- column3
- from
- table_name
- Order by
- column_name;
Example
- select
- customer_id,
- customer_name,
- product_code
- from
- customer
- order by
- customer_id;
Now, you will create our cross tabulation query using the following pivot clause.
Syntax
- select
- *
- from
- (
- select
- column1,
- column2
- from
- table_name
- ) PIVOT (
- COUNT (column_name) for column_name IN(column_values)
- )
- order by
- customer_name;
- select
- *
- from
- (
- select
- customer_name,
- quantity
- from
- customer
- ) PIVOT (
- COUNT(quantity) for quantity IN (10, 20, 30)
- )
- order by
- customer_name;
Now, let’s explain, how Pivot clause works.
Let’s break apart Pivot clause.
First, we want to specify what fields to include in our cross tabulation.
In this example, we want to include the customer_name and product_code fields. This is done by the portion of the statement, given below-
- (
- Select
- customer_name,
- product_code
- from
- customer
- )
You can list the columns to be included in any order.
Now, we will specify aggregate function and Pivot values.
You can use any aggregate function such as SUM, COUNT, MIN, MAX or AVG functions.
In this example, we will choose the count function. This function will count the number of product_code values.
Statement
- pivot (
- count (product_id)
Finally, we need to specify, what are pivot values.
In this example, we are going to return only the following product_code values. These values become a column heading in our cross-tabulation query.
Statement
- for product_code IN(10, 20, 40)
- )
Summary
Thus, we learnt, Pivot Clause allows you to write a cross-tabulation query in Oracle. Pivot clause takes the data in separate row, aggregates it and converts it into the column. We learnt, how to use this clause with the examples.