Back to normal view: https://oracle-base.com/articles/11g/pivot-and-unpivot-operators-11gr1

PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1

This article shows how to use the new PIVOT and UNPIVOT operators in 11g, as well as giving a pre-11g solution to the same problems.

Related articles.

PIVOT

The PIVOT operator takes data in separate rows, aggregates it and converts it into columns. To see the PIVOT operator in action we need to create a test table.

create table pivot_test (
  id            number,
  customer_id   number,
  product_code  varchar2(5),
  quantity      number
);

insert into pivot_test values (1, 1, 'A', 10);
insert into pivot_test values (2, 1, 'B', 20);
insert into pivot_test values (3, 1, 'C', 30);
insert into pivot_test values (4, 2, 'A', 40);
insert into pivot_test values (5, 2, 'C', 50);
insert into pivot_test values (6, 3, 'A', 60);
insert into pivot_test values (7, 3, 'B', 70);
insert into pivot_test values (8, 3, 'C', 80);
insert into pivot_test values (9, 3, 'D', 90);
insert into pivot_test values (10, 4, 'A', 100);
commit;

So our test data starts off looking like this.

select * from pivot_test;

        ID CUSTOMER_ID PRODU   QUANTITY
---------- ----------- ----- ----------
         1           1 A             10
         2           1 B             20
         3           1 C             30
         4           2 A             40
         5           2 C             50
         6           3 A             60
         7           3 B             70
         8           3 C             80
         9           3 D             90
        10           4 A            100

10 rows selected.

SQL>

In its basic form the PIVOT operator is quite limited. We are forced to list the required values to PIVOT using the IN clause.

select *
from   (select product_code, quantity
        from   pivot_test)
pivot  (sum(quantity) as sum_quantity for (product_code) in ('A' as a, 'B' as b, 'C' as c));

A_SUM_QUANTITY B_SUM_QUANTITY C_SUM_QUANTITY
-------------- -------------- --------------
           210             90            160

1 row selected.

SQL>

If we want to break it down by customer, we simply include the CUSTOMER_ID column in the initial select list.

select *
from   (select customer_id, product_code, quantity
        from   pivot_test)
pivot  (sum(quantity) as sum_quantity for (product_code) in ('A' as a, 'B' as b, 'C' as c))
order by customer_id;

CUSTOMER_ID A_SUM_QUANTITY B_SUM_QUANTITY C_SUM_QUANTITY
----------- -------------- -------------- --------------
          1             10             20             30
          2             40                            50
          3             60             70             80
          4            100

4 rows selected.

SQL>

Prior to 11g we could accomplish a similar result using the DECODE function combined with aggregate functions.

select sum(decode(product_code, 'A', quantity, 0)) as a_sum_quantity,
       sum(decode(product_code, 'B', quantity, 0)) as b_sum_quantity,
       sum(decode(product_code, 'C', quantity, 0)) as c_sum_quantity
from   pivot_test
order by customer_id;

A_SUM_QUANTITY B_SUM_QUANTITY C_SUM_QUANTITY
-------------- -------------- --------------
           210             90            160

1 row selected.

SQL>

select customer_id,
       sum(decode(product_code, 'A', quantity, 0)) as a_sum_quantity,
       sum(decode(product_code, 'B', quantity, 0)) as b_sum_quantity,
       sum(decode(product_code, 'C', quantity, 0)) as c_sum_quantity
from   pivot_test
group by customer_id
order by customer_id;

CUSTOMER_ID A_SUM_QUANTITY B_SUM_QUANTITY C_SUM_QUANTITY
----------- -------------- -------------- --------------
          1             10             20             30
          2             40              0             50
          3             60             70             80
          4            100              0              0

4 rows selected.

SQL>

Adding the XML keyword to the PIVOT operator allows us to convert the generated pivot results to XML format. It also makes the PIVOT a little more flexible, allowing us to replace the hard coded IN clause with a subquery, or the ANY wildcard.

set long 10000

select *
from   (select product_code, quantity
        from   pivot_test)
pivot xml (sum(quantity) as sum_quantity for (product_code) in (select distinct product_code 
                                                                from   pivot_test
                                                                where  id < 10));

product_code_XML
----------------------------------------------------------------------------------------------------
<PivotSet><item><column name = "PRODUCT_CODE">A</column><column name = "SUM_QUANTITY">210</column></
item><item><column name = "PRODUCT_CODE">B</column><column name = "SUM_QUANTITY">90</column></item><
item><column name = "PRODUCT_CODE">C</column><column name = "SUM_QUANTITY">160</column></item><item>
<column name = "PRODUCT_CODE">D</column><column name = "SUM_QUANTITY">90</column></item></PivotSet>

1 row selected.

SQL>

select *
from   (select product_code, quantity
        from   pivot_test)
pivot xml (sum(quantity) as sum_quantity for (product_code) in (any));

product_code_XML
----------------------------------------------------------------------------------------------------
<PivotSet><item><column name = "PRODUCT_CODE">A</column><column name = "SUM_QUANTITY">210</column></
item><item><column name = "PRODUCT_CODE">B</column><column name = "SUM_QUANTITY">90</column></item><
item><column name = "PRODUCT_CODE">C</column><column name = "SUM_QUANTITY">160</column></item><item>
<column name = "PRODUCT_CODE">D</column><column name = "SUM_QUANTITY">90</column></item></PivotSet>

1 row selected.

SQL>

Once again, the results can be broken down by customer, with each customers XML presented as a separate row.

set long 10000

select *
from   (select customer_id, product_code, quantity
        from   pivot_test)
pivot xml (sum(quantity) as sum_quantity for (product_code) in (select distinct product_code 
                                                                from   pivot_test));

CUSTOMER_ID
-----------
PRODUCT_CODE_XML
----------------------------------------------------------------------------------------------------
          1
<PivotSet><item><column name = "PRODUCT_CODE">A</column><column name = "SUM_QUANTITY">10</column></i
tem><item><column name = "PRODUCT_CODE">B</column><column name = "SUM_QUANTITY">20</column></item><i
tem><column name = "PRODUCT_CODE">C</column><column name = "SUM_QUANTITY">30</column></item><item><c
olumn name = "PRODUCT_CODE">D</column><column name = "SUM_QUANTITY"></column></item></PivotSet>

          2
<PivotSet><item><column name = "PRODUCT_CODE">A</column><column name = "SUM_QUANTITY">40</column></i
tem><item><column name = "PRODUCT_CODE">B</column><column name = "SUM_QUANTITY"></column></item><ite

CUSTOMER_ID
-----------
PRODUCT_CODE_XML
----------------------------------------------------------------------------------------------------
m><column name = "PRODUCT_CODE">C</column><column name = "SUM_QUANTITY">50</column></item><item><col
umn name = "PRODUCT_CODE">D</column><column name = "SUM_QUANTITY"></column></item></PivotSet>

          3
<PivotSet><item><column name = "PRODUCT_CODE">A</column><column name = "SUM_QUANTITY">60</column></i
tem><item><column name = "PRODUCT_CODE">B</column><column name = "SUM_QUANTITY">70</column></item><i
tem><column name = "PRODUCT_CODE">C</column><column name = "SUM_QUANTITY">80</column></item><item><c
olumn name = "PRODUCT_CODE">D</column><column name = "SUM_QUANTITY">90</column></item></PivotSet>


CUSTOMER_ID
-----------
PRODUCT_CODE_XML
----------------------------------------------------------------------------------------------------
          4
<PivotSet><item><column name = "PRODUCT_CODE">A</column><column name = "SUM_QUANTITY">100</column></
item><item><column name = "PRODUCT_CODE">B</column><column name = "SUM_QUANTITY"></column></item><it
em><column name = "PRODUCT_CODE">C</column><column name = "SUM_QUANTITY"></column></item><item><colu
mn name = "PRODUCT_CODE">D</column><column name = "SUM_QUANTITY"></column></item></PivotSet>


4 rows selected.

SQL>

UNPIVOT

The UNPIVOT operator converts column-based data into separate rows. To see the UNPIVOT operator in action we need to create a test table.

create table unpivot_test (
  id              number,
  customer_id     number,
  product_code_a  number,
  product_code_b  number,
  product_code_c  number,
  product_code_d  number
);

insert into unpivot_test values (1, 101, 10, 20, 30, null);
insert into unpivot_test values (2, 102, 40, null, 50, null);
insert into unpivot_test values (3, 103, 60, 70, 80, 90);
insert into unpivot_test values (4, 104, 100, null, null, null);
commit;

So our test data starts off looking like this.

select * from unpivot_test;

        ID CUSTOMER_ID PRODUCT_CODE_A PRODUCT_CODE_B PRODUCT_CODE_C PRODUCT_CODE_D
---------- ----------- -------------- -------------- -------------- --------------
         1         101             10             20             30
         2         102             40                            50
         3         103             60             70             80             90
         4         104            100

4 rows selected.

SQL>

The UNPIVOT operator converts this column-based data into individual rows.

select *
from   unpivot_test
unpivot (quantity for product_code in (product_code_a as 'A', product_code_b as 'B', product_code_c as 'C', product_code_d as 'D'));

        ID CUSTOMER_ID P   QUANTITY
---------- ----------- - ----------
         1         101 A         10
         1         101 B         20
         1         101 C         30
         2         102 A         40
         2         102 C         50
         3         103 A         60
         3         103 B         70
         3         103 C         80
         3         103 D         90
         4         104 A        100

10 rows selected.

SQL>

There are several things to note about the query:

The following query shows the inclusion of the INCLUDE NULLS clause.

select *
from   unpivot_test
unpivot include nulls (quantity for product_code in (product_code_a as 'A', product_code_b as 'B', product_code_c as 'C', product_code_d as 'D'));

        ID CUSTOMER_ID P   QUANTITY
---------- ----------- - ----------
         1         101 A         10
         1         101 B         20
         1         101 C         30
         1         101 D
         2         102 A         40
         2         102 B
         2         102 C         50
         2         102 D
         3         103 A         60
         3         103 B         70
         3         103 C         80

        ID CUSTOMER_ID P   QUANTITY
---------- ----------- - ----------
         3         103 D         90
         4         104 A        100
         4         104 B
         4         104 C
         4         104 D

16 rows selected.

SQL>

Prior to 11g, we can get the same result using the DECODE function and a pivot table with the correct number of rows. In the following example we use the CONNECT BY clause in a query from dual to generate the correct number of rows for the unpivot operation.

select id,
       customer_id,
       decode(unpivot_row, 1, 'A',
                           2, 'B',
                           3, 'C',
                           4, 'D',
                           'N/A') as product_code,
       decode(unpivot_row, 1, product_code_a,
                           2, product_code_b,
                           3, product_code_c,
                           4, product_code_d,
                           'N/A') as quantity
from   unpivot_test,
       (select level as unpivot_row from dual connect by level <= 4)
order by 1,2,3;

        ID CUSTOMER_ID PRO   QUANTITY
---------- ----------- --- ----------
         1         101 A           10
         1         101 B           20
         1         101 C           30
         1         101 D
         2         102 A           40
         2         102 B
         2         102 C           50
         2         102 D
         3         103 A           60
         3         103 B           70
         3         103 C           80

        ID CUSTOMER_ID PRO   QUANTITY
---------- ----------- --- ----------
         3         103 D           90
         4         104 A          100
         4         104 B
         4         104 C
         4         104 D

16 rows selected.

SQL>

For more information see:

Hope this helps. Regards Tim...

Back to the Top.

Back to normal view: https://oracle-base.com/articles/11g/pivot-and-unpivot-operators-11gr1