This Oracle tutorial explains how to use the Oracle PIVOT clause with syntax and examples.
The Oracle PIVOT clause allows you to write a cross-tabulation query starting in Oracle 11g. This means that you can aggregate your results and rotate rows into columns.
The syntax for the PIVOT clause in Oracle/PLSQL is:
It can be a function such as SUM, COUNT, MIN, MAX, or AVG functions.
A list of values for column2 to pivot into headings in the cross-tabulation query results.
It can be used instead of a list of values. In this case, the results of the subquery would be used to determine the values for column2 to pivot into headings in the cross-tabulation query results.
The PIVOT clause can be used in the following versions of Oracle/PLSQL:
Let's look at how to use the PIVOT clause in Oracle.
We will base our example on a table called orders with the following definition:
To show you the data for this example, we will select the records from the orders table with the following SELECT statement:
These are the records in the orders table. We will be using these records to demonstrate how the PIVOT clause works:
order_id | customer_ref | product_id |
---|---|---|
50001 | SMITH | 10 |
50002 | SMITH | 20 |
50003 | ANDERSON | 30 |
50004 | ANDERSON | 40 |
50005 | JONES | 10 |
50006 | JONES | 20 |
50007 | SMITH | 20 |
50008 | SMITH | 10 |
50009 | SMITH | 20 |
Now, let's create our cross-tabulation query using the following PIVOT clause:
In this example, the PIVOT clause would return the following results:
customer_ref | 10 | 20 | 30 |
---|---|---|---|
ANDERSON | 0 | 0 | 1 |
JONES | 1 | 1 | 0 |
SMITH | 2 | 3 | 0 |
Now, let's break apart the PIVOT clause and explain how it worked.
First, we want to specify what fields to include in our cross tabulation. In this example, we want to include the customer_ref and product_id fields. This is done by the following portion of the statement:
You can list the columns to be included in any order.
Next, we need to specify what aggregate function to use when creating our cross-tabulation query. You can use any aggregate such as SUM, COUNT, MIN, MAX, or AVG functions.
In this example, we are going to use the COUNT function. This will count the number of product_id values that meet our criteria. This is done by the following portion of the statement:
Finally, we need to specify what pivot values to include in our results. These will be used as the column headings in our cross-tabulation query. You can use either be a list of values enclosed in parentheses or a subquery to specify the pivot values.
In this example, we are going to return only the following product_id values: 10, 20, 30. These values will become our column headings in our cross-tabulation query. Also, note that these values are a finite list of the product_id values and will not necessarily contain all possible values.
This is done by the following portion of the statement:
Now when we put it all together, we get the following pivot table:
customer_ref | 10 | 20 | 30 |
---|---|---|---|
ANDERSON | 0 | 0 | 1 |
JONES | 1 | 1 | 0 |
SMITH | 2 | 3 | 0 |