Oracle Database 11g: BI and Data Warehousing

Student Guide

D52358GC10 Edition 1.0 October 2007 PRODUCTION

Copyright © 2007, Oracle. All rights reserved. This documentation contains proprietary information of Oracle Corporation. It is provided under a license agreement containing restrictions on use and disclosure and is also protected by copyright law. Reverse engineering of the software is prohibited. If this documentation is delivered to a U.S. Government Agency of the Department of Defense, then it is delivered with Restricted Rights and the following legend is applicable: Restricted Rights Legend Use, duplication or disclosure by the Government is subject to restrictions for commercial computer software and shall be deemed to be Restricted Rights software under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013, Rights in Technical Data and Computer Software (October 1988). This material or any portion of it may not be copied in any form or by any means without the express prior written permission of the Education Products group of Oracle Corporation. Any other copying is a violation of copyright law and may result in civil and/or criminal penalties. If this documentation is delivered to a U.S. Government Agency not within the Department of Defense, then it is delivered with “Restricted Rights,” as defined in FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987). The information in this document is subject to change without notice. If you find any problems in the documentation, please report them in writing to Worldwide Education Services, Oracle Corporation, 500 Oracle Parkway, Box SB-6, Redwood Shores, CA 94065. Oracle Corporation does not warrant that this document is error-free. Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Author Lauran Serhal, Jim Womack, Jean-Francois Verrier Technical Contributors and Reviewers John Haydu, Lei Sheng, Lilian Hobbs, Paul Lane, Diana Lorentz, Christine Jeal This book was published using:

oracletutor

Table of Contents
Partitioning and Storage-Related Enhancements.........................................................................................1-2 Chapter 1Partitioning and Storage-Related Enhancements ...........................................................................1-2 Objectives......................................................................................................................................................1-3 Oracle Partitioning ........................................................................................................................................1-4 11g Partitioning Enhancements.....................................................................................................................1-5 Interval Partitioning.......................................................................................................................................1-6 Interval Partitioning: Example ......................................................................................................................1-7 Moving the Transition Point: Example .........................................................................................................1-8 System Partitioning .......................................................................................................................................1-9 System Partitioning: Example .......................................................................................................................1-10 System Partitioning: Guidelines ....................................................................................................................1-11 Virtual Column–Based Partitioning ..............................................................................................................1-12 Virtual Column–Based Partitioning: Example ..............................................................................................1-13 Reference Partitioning...................................................................................................................................1-14 Reference Partitioning: Benefit .....................................................................................................................1-15 Reference Partitioning: Example...................................................................................................................1-16 Composite Partitioning Enhancements..........................................................................................................1-18 Range-Range Partitioning: Example .............................................................................................................1-19 Table Compression: Overview ......................................................................................................................1-20 Table Compression Concepts ........................................................................................................................1-21 Using Table Compression .............................................................................................................................1-22 Summary .......................................................................................................................................................1-23 Using the New and Updated Materialized Views (MV) Catalog Views......................................................2-2 Chapter 2Using the New and Updated Materialized Views (MV) Catalog Views .......................................2-2 Objectives......................................................................................................................................................2-3 The New and Updated MV Catalog Views ...................................................................................................2-4 PCT Catalog Views Showing Staleness Corresponding to Base Partitions ..................................................2-5 The New Columns in the USER/ALL/DBA_MVIEWS Catalog View .......................................................2-7 The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS Catalog View Extension ...................................2-8 The New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_PARTITION ...........................................2-9 The New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION....................................2-10 Refresh Performance Improvements in Oracle Database 11g ......................................................................2-11 Summary .......................................................................................................................................................2-13 General Query Rewrite Enhancements.........................................................................................................3-2 Chapter 3General Query Rewrite Enhancements ..........................................................................................3-2 Objectives......................................................................................................................................................3-3 Using Summaries to Improve Performance...................................................................................................3-4 Summary Management..................................................................................................................................3-6 Summary Management Components ............................................................................................................3-7 Using Summary Management .......................................................................................................................3-8 Query Rewrite: Overview .............................................................................................................................3-9 Cost-Based Query Rewrite Process...............................................................................................................3-10 What Can Be Rewritten? ...............................................................................................................................3-11 Query Rewrite Enhancement to Support Queries Containing Inline Views.................................................3-12 When Are Two Inline Views Equivalent?.....................................................................................................3-13 An MV Inline View Text Matches a Query's Inline View Text: Example...................................................3-14 An MV Inline View Text Is Equivalent to a Query's Inline View Text: Example .......................................3-15 Transforming and Rewriting the Query from the Two Previous Examples .................................................3-16 Inline View Rewrite Guidelines ....................................................................................................................3-17 Did Query Rewrite Occur?............................................................................................................................3-18 Query Rewrite Using Remote Tables in Oracle Database 11g.....................................................................3-19 Query Rewrite Using Remote Tables: Example............................................................................................3-20 Summary .......................................................................................................................................................3-21 Performing Pivoting and Unpivoting Operations.........................................................................................4-2
Copyright © 2007, Oracle. All rights reserved.

Oracle Database 11g: BI and Data Warehousing Table of Contents i

...............4-3 The Order Entry (OE) Schema Used in This Lesson ..........................................................4-31 Creating a New Pivot Table: Example ................................................................................................................................................................................................4-12 Selecting the SALES_VIEW Data .............................................4-22 Using the XML Keyword to Specify Pivot Values: Two Methods ...4-14 Pivoting the QUARTER Column in the SH Schema: Example ..................................................4-37 Summary ....................................................................................4-5 The Sales History (SH) Schema Used in this eStudy ............4-28 Using the UNPIVOT Clause ............................................................................................................................................4-27 Using the UNPIVOT Operator....................4-32 Unpivoting the QUARTER Column in the SH Schema: Example ............4-21 Distinguishing PIVOT-Generated Nulls from Nulls in the Source Data.........................................................................................................................................................................................................................................................................4-18 Pivoting on Multiple Columns .................................................................................4-7 The PIVOT and UNPIVOT Clauses of the SELECT Statement.........................................................................................................................................4-35 Unpivoting Multiple Aggregations in the SH Schema: Example........................................................................................4-26 Unpivoting the QUARTER Column: Conceptual Example ..............4-2 Objectives.....................................................................................4-34 Unpivoting Multiple Columns in the SH Schema: Example ..................................................................................................................................4-30 The UNPIVOT Clause Syntax ..........................................................................................4-10 Creating a New View: Example ...........................4-9 The PIVOT Clause Syntax ...........................4-25 Specifying Pivot Values: Using Subqueries...............................................................................................................................................................................................................................................................................4-6 The Benefits of Using Pivoting Operations..................................................................................................................................4-4 The Sales History (SH) Schema Used in This Lesson .........4-24 Specifying Pivot Values: Using the ANY Keyword ...............................................4-8 Pivoting on the QUARTER Column: Conceptual Example...... Oracle.................................4-29 The Data Types of the Value Columns in an UNPIVOT Operation .............................................................................................................................................................4-33 Unpivoting the ORDER_MODE Column in the OE Schema: Example ......................... Oracle Database 11g: BI and Data Warehousing Table of Contents ii ............................................................................................................................................................................................................ All rights reserved.............................................................................................................................................................................Chapter 4Performing Pivoting and Unpivoting Operations ..............4-19 Pivoting Using Multiple Aggregations............4-39 Copyright © 2007...........4-16 Pivoting on the ORDER_MODE Column in the OE Schema: Example.............................................

Page 1 . Oracle. Partitioning and Storage-Related Enhancements Chapter 0 .Partitioning and StorageRelated Enhancements Copyright © 2007. All rights reserved.

Chapter 1Partitioning and Storage-Related Enhancements Partitioning and Storage-Related Enhancements Copyright © 2007. Oracle. Partitioning and Storage-Related Enhancements Chapter 1 . All rights reserved.Page 2 .

you should be able to: • Implement the new partitioning methods • Employ data compression Copyright © 2007. Partitioning and Storage-Related Enhancements Chapter 1 .Page 3 .Objectives Objectives After completing this lesson. Oracle. All rights reserved.

do not forget about the rest. and availability.Page 4 . drop. Partitioning must address all business-relevant areas of performance.Oracle Partitioning Oracle Partitioning Core functionality Performance Static partition pruning Partitionwise joins Dynamic pruning Manageability Basic maintenance operations: add. Copyright © 2007. manageability. Partitioning and Storage-Related Enhancements Chapter 1 . Note: REF partitioning enables pruning and partitionwise joins against child tables. Oracle. exchange Merge operation Global index maintenance Oracle8 Range partitioning Global range indexes Hash and composite range-hash partitioning List partitioning Composite range-list partitioning Global hash indexes 1M partitions per table More composite choices REF Partitioning Virtual Column Partitioning Oracle8i Oracle9i Oracle9i R2 Oracle10g Oracle10g R2 Oracle Database 11g Fast partition split Local Index maintenance Multidimensional pruning Fast drop table Interval Partitioning Partition Advisor The table above summarizes the 10 years of partitioning development at Oracle. All rights reserved. Though performance seems to be the most visible improvement.

Partitioning enables faster data access within an Oracle database. On import.11g Partitioning Enhancements 11g Partitioning Enhancements • • • • • Interval partitioning System partitioning Composite partitioning enhancements Virtual column-based partitioning Reference partitioning • Data Pump Enhancement: – Single Partition Transportable Tablespace Partitioning allows the DBA to employ a “divide and conquer” methodology for managing database tables. These enhancements include: • Addition of interval partitioning • Addition of system partitioning • Composite partitioning enhancements • Addition of virtual column-based partitioning • Addition of reference partitioning Data Pump Enhancement: You can now export one or more partitions of a table without having to move the entire table. or promote each into a separate table. All rights reserved. especially as those tables grow. you can choose to load partitions as is.1) guide for more information on Data Pump. With the introduction of Oracle Database 11g. partitioning can speed up data access by orders of magnitude. the DBA will find a useful assortment of partitioning enhancements. Partitioning and Storage-Related Enhancements Chapter 1 . Please refer to the Oracle® Database Utilities 11g Release 1 (11. Whether a database has 10 GB or 10 TB of data. without unduly impacting administrative or hardware resources. Copyright © 2007. merge them into a single table.Page 5 . Oracle. Partitioned tables allow a database to scale for very large datasets while maintaining consistent performance.

• At least one range partition must be created. • Interval partitioning is not supported for index-organized tables. which is called the transition point. Interval partitioning automates this operation by creating partitions on demand. You must specify at least one range partition. and it must be of NUMBER or DATE type. • Partitions of a specified interval are created when inserted data exceeds all of the range partitions. • You cannot create a domain index on an interval-partitioned table. which instructs the database to automatically create partitions of a specified interval when data inserted into the table exceeds all of the range partitions. When using interval partitioning. The problem is explicitly defining the bounds for each partition does not scale as the number of partitions grow. and the database creates interval partitions for data beyond that transition point. The range partitioning key value determines the high value of the range partitions. the DBA was required to explicitly define the range of values for each partition. Interval partitioning fully automates the creation of range partitions. Interval partitioning is an extension of range partitioning. Managing the creation of new partitions can be a cumbersome and highly repetitive task. consider the following restrictions: • You can specify only one partitioning key column. Partitioning and Storage-Related Enhancements Chapter 1 . Before the introduction of interval partitioning. Oracle. such as adding new daily partitions.Interval Partitioning Interval Partitioning • Interval partitioning is an extension of range partitioning.Page 6 . • Interval partitioning automates the creation of range partitions. Copyright © 2007. This is especially true for predictable additions of partitions covering small ranges. All rights reserved.

'dd-mm-yyyy').Page 7 .SALES_INTERVAL PARTITION BY RANGE (time_id) INTERVAL (NUMTOYMINTERVAL(1. The only argument to the INTERVAL clause is a constant of the interval type.tbs4) ( PARTITION P1 values less than (TO_DATE('1-1-2005'.'dd-mm-yyyy'))) AS SELECT * FROM SH. PARTITION P2 values less than (TO_DATE('1-1-2006'. Partition Pi1 is automatically created using this information when a row with a value corresponding to January 2007 is inserted into the table.'dd-mm-yyyy')).tbs3. you can specify only one partitioning key column. Partitioning and Storage-Related Enhancements Chapter 1 .” partitions be created with a width of one month. and it must be of DATE or NUMBER type. You can use the optional STORE IN clause of the INTERVAL clause to specify one or more tablespaces into which the database will store interval partition data in a round-robin fashion. The original CREATE TABLE statement specifies four partitions with varying widths.Interval Partitioning: Example Interval Partitioning: Example CREATE TABLE SH. Copyright © 2007. P3 and all partitions below it (P1 and P2 in this example) are in the range section. which illustrates the creation of an interval-partitioned table.'dd-mm-yyyy')). Automatically created when you insert data P1 P2 Range section P3 Pi1 … Pin Interval section Transition point Consider the example above.tbs2. This portion of the table is range-partitioned. Currently. The high bound of partition P3 represents a transition point. PARTITION P3 values less than (TO_DATE('1-1-2007'. These partitions are intervalpartitioned. All rights reserved. It also specifies that above the transition point of “11-2007. whereas all partitions above it fall into the interval section.SALES WHERE TIME_ID < TO_DATE('1-1-2007'.'month')) STORE IN (tbs1. Oracle.

Oracle. or hash. Copyright © 2007. split. You must merge two adjacent partitions. The new extended partition syntax is used to designate a partition without needing to know the partition name. This syntax works for all cases when you have to reference a partition. You then decide to merge the last two partitions of the year 2007 using the ALTER TABLE statement shown in the slide. The interval is defined to be one month. They are shown in the graphic.Moving the Transition Point: Example Moving the Transition Point: Example PREVIOUS < 01/01/07 Transition point INSERT INTO orders_interval (…). It supports all operations such as drop. list.Page 8 .for(TO_DATE('15-11-2007'. The bottom part of the graphic shows you the new range section that now contains three partitions. whether it is range. and so on. some orders are inserted. which holds orders from before 2007. The syntax uses an expression that must represent a possible value for the partition in question.'dd-mm-yyyy')). the existing intervals remain unaffected. You can then move the resulting partitions to a different storage for ILM purposes. Partitioning and Storage-Related Enhancements Chapter 1 . PREVIOUS < 01/01/07 Not yet materialized Transition point SYS_Px < 01/08/07 SYS_Py < 01/11/07 SYS_Pz < 01/12/07 SYS_Pt < 01/03/08 alter table orders_interval merge partitions for(TO_DATE('15-10-2007'. They are automatically named according to certain system rules. The example assumes that you created a table called ORDERS_INTERVAL that has initially one range partition called PREVIOUS.'dd-mm-yyyy')) into partition sys_pz. and it is assumed that four partitions are created. Then during the year 2007 and 2008. you can see that the transition point moved. PREVIOUS < 01/01/07 SYS_Px < 01/08/07 SYS_Pz < 01/12/07 Transition point SYS_Pt < 01/03/08 The graphic in the slide shows you a typical Information Lifecycle Management (ILM) scenario where after one year of automated partition creation. All rights reserved. As a result of your MERGE operation. interval. you merge the created partitions (SYS_Py and SYS_Pz in the example) to move the transition point. merge. Note: You can change the interval of an interval-partitioned table.

Page 9 . Specifically. The most fundamental difference between system partitioning and other methods is that system partitioning does not have any partitioning keys. Instead. The database simply provides the ability to break down a table into meaningless partitions. System-partitioned tables provide the manageability advantages of equipartitioning. a nested table can be created as a system-partitioned table that has the same number of partitions as the base table. All other aspects of partitioning are controlled by the application. the distribution or mapping of the rows to a particular partition is not implicit. and so on. A domain index can be backed up by a system-partitioned table that has the same number of partitions as the base table. availability. and manageability). partitionwise joins.System Partitioning System Partitioning System partitioning: • Enables application-controlled partitioning for selected tables • Provides the benefits of partitioning but the partitioning and data placement are controlled by the application • Does not employ partitioning keys like other partitioning methods • Does not support partition pruning in the traditional sense System partitioning enables application-controlled partitioning for arbitrary tables. Because system-partitioned tables do not have a partitioning key. but the partitioning and actual data placement are controlled by the application. System partitioning provides the well-known benefits of partitioning (scalability. the usual performance benefits of partitioned tables are not available for system-partitioned tables. Oracle. Partitioning and Storage-Related Enhancements Chapter 1 . the user specifies the partition to which a row maps by using partition-extended syntax when inserting a row. For example. This is mainly useful when you develop your own partitioned domain indexes. Copyright © 2007. Consequently. there is no support for traditional partition pruning. All rights reserved. Partition pruning is achieved by accessing the same partitions in the system-partitioned tables as those that were accessed in the base table.

2).5) can be inserted into any one of the four partitions given in the example. Oracle. alter table systab merge partitions p1. Partitioning and Storage-Related Enhancements Chapter 1 . this example highlights the fact that there is no implicit mapping from rows to any partition. INSERT and MERGE statements must use partition-extended syntax to identify a particular partition that a row should go into. The syntax in the slide example creates a table with four partitions.System Partitioning: Example System Partitioning: Example CREATE TABLE PARTITION BY ( PARTITION PARTITION PARTITION PARTITION ). All rights reserved. tbs_2. Copyright © 2007. the entire table is scanned to execute the operation.Page 10 . Each partition can have different physical attributes. because there is no partition pruning. if the partition-extended syntax is omitted.5). For example. INSERT INTO systab PARTITION (p2) VALUES (150. the value (4. However. systab (c1 integer.p2 into partition p1. tbs_4 INSERT INTO systab PARTITION (p1) VALUES (4. Deletes and updates do not require the partition-extended syntax. Again. c2 integer) SYSTEM p1 p2 p3 p4 TABLESPACE TABLESPACE TABLESPACE TABLESPACE tbs_1. tbs_3.

Page 11 . you should first create the table and then insert rows into each partition. All rights reserved. Instead. • SPLIT PARTITION operations Copyright © 2007. . the following operations are not supported for system partitioning: • Unique local indexes are not supported because they require a partitioning key. . • CREATE TABLE AS SELECT is not supported because there is no partitioning method.System Partitioning: Guidelines System Partitioning: Guidelines The following operations are supported for systempartitioned tables: • Partition maintenance operations and other data definition language (DDL) operations • Creation of local indexes • Creation of local bitmapped indexes • Creation of global indexes • All DML operations • INSERT. Because of the peculiar requirements of system partitioning. . SELECT with partition-extended syntax: INSERT INTO <table_name> PARTITION(<partition-name>) <subquery> The following operations are supported for system-partitioned tables: • Partition maintenance operations and other DDLs (see exceptions below) • Creation of local indexes • Creation of local bitmapped indexes • Creation of global indexes • All data manipulation language (DML) operations • INSERT. Oracle.SELECT with partition-extended syntax. Partitioning and Storage-Related Enhancements Chapter 1 . . It is not possible to distribute rows to partitions.

business requirements to logically partition objects do not match existing columns in a one-to-one manner. With the introduction of Oracle Database 11g.Virtual Column–Based Partitioning Virtual Column–Based Partitioning • Virtual column values are derived by the evaluation of a function or expression. • … total_compensation as (salary *( 1+commission_pct)) • Virtual column values are not physically stored in the table row on disk. Partitioning and Storage-Related Enhancements Chapter 1 . They can be indexed. These columns can be specified during a CREATE or ALTER table operation. Oracle. and used in queries. CREATE TABLE employees (employee_id number(6) not null. The functions or expressions describing the virtual columns should be deterministic and pure. • Tables and indexes can be partitioned on a virtual column and statistics can be gathered upon them. but are evaluated on demand. DML. Tables and indexes can be partitioned on a virtual column and statistics can be gathered upon them. Virtual columns share the same SQL namespace as other real table columns and conform to the data type of the underlying expression that describes it. Copyright © 2007. meaning the same set of input values should return the same output values. partitioning has been enhanced to allow a partitioning strategy defined on virtual columns. thus enabling a more comprehensive match of the business requirements. These columns can be used in queries like any other table columns. You can use virtual column partitioning to partition key columns defined on virtual columns of a table. and used in queries. • Virtual columns can be defined within a CREATE or ALTER table operation. Frequently. rather they are evaluated on demand. DML. elegant. and consistent mechanism of accessing expressions in a SQL statement. The values for virtual columns are not physically stored in the table row on disk. Virtual columns can be used like any other table columns.Page 12 . Columns of a table whose values are derived by computation of a function or an expression are known as virtual columns. and DDL statements like other table column types. and DDL statements. thereby providing a simple. All rights reserved. • Virtual columns can be indexed.

job_id varchar2(10) not null. PARTITION p2 VALUES LESS THAN (100000). and annotates the join properly when it decides to use it. phone_number varchar2(20). Partition pruning takes place for virtual column partition keys when the predicates on the partitioning key are of the following types: • Equality or Like • List • Range • Partition-extended names Given a join operation between two tables. decides whether to use it or not. Consider the example in the slide. manager_id number(6). this definition includes the equivalence of the virtual expression on which the tables were partitioned. Oracle. commission_pct number(2. Copyright © 2007. The total_compensation column is a virtual column calculated by multiplying the value of salary by the commission_pct plus one.Virtual Column–Based Partitioning: Example Virtual Column–Based Partitioning: Example CREATE TABLE employees (employee_id number(6) not null. last_name varchar2(40) not null. the optimizer relies on the definition of equipartitioning of two objects. All rights reserved.2). total_compensation as (salary *( 1+commission_pct)) ) PARTITION BY RANGE (total_compensation) ( PARTITION p1 VALUES LESS THAN (50000). PARTITION p4 VALUES LESS THAN (MAXVALUE) ).Page 13 . PARTITION p3 VALUES LESS THAN (150000). This applies to both serial and parallel cases. hire_date date not null. The next statement declares total_compensation (a virtual column) to be the partitioning key of the EMPLOYEES table. emailvarchar2(25). salary number(8. The EMPLOYEES table is created using the standard CREATE TABLE syntax. first_name varchar2(30). To recognize full partitionwise joins. the optimizer recognizes when a partitionwise join (full or partial) is applicable.2). Partitioning and Storage-Related Enhancements Chapter 1 . department_id number(4).

Partitioning and Storage-Related Enhancements Chapter 1 . The benefit of this is that tables with a parent/child relationship can be logically equipartitioned by inheriting the partitioning key from the parent table without duplicating the key columns. All rights reserved. The logical dependency also automatically cascades partition maintenance operations. Reference partitioning provides the ability to partition a table based on the partitioning scheme of the table referenced in its referential constraint. which is enforced by active primary key or foreign key constraints. making application development easier and less error prone. • Tables with a parent/child relationship can be equipartitioned by inheriting the partitioning key from the parent table without duplicating the key columns.Page 14 . • The partitioning key is resolved through an existing parent/child relationship. Oracle. The partitioning key is resolved through an existing parent/child relationship.Reference Partitioning Reference Partitioning • A table can now be partitioned based on the partitioning method of a table referenced in its referential constraint. • Partitions are automatically maintained. • The partitioning key is enforced by active primary key or foreign key constraints. Copyright © 2007.

that are equipartitioned on the ORDER_DATE column.Reference Partitioning: Benefit Reference Partitioning: Benefit Without using reference partitioning Reference partitioning … Range (ORDER_DATE) Primary key (ORDER_ID) … Table ORDERS Table ORDER_ITEMS … Range (ORDER_DATE) Foreign key (ORDER_ID) … Redundant storage/maintenance of ORDER_DATE Partition key inherited through PK/FK relationship As illustrated in the slide. However. The left part of the graphic shows you the situation where you have two tables. Partitioning and Storage-Related Enhancements Chapter 1 . ORDERS and ORDER_ITEMS. With previous releases.Page 15 . you can see the benefit of using reference partitioning. reference partitioning has the benefit that query predicates can be different and partitionwise joins still work—for example. both tables need to define the ORDER_DATE column. Note: This partitioning method can be useful for nested table partitioning. In that case. you no longer need to define the ORDER_DATE column in the ORDER_ITEMS table. partitioning on ORDER_DATE and search on ORDER_ID. both partitioning and predicates had to be identical for a partitionwise join to work. All rights reserved. Oracle. Copyright © 2007. The partition key of the ORDER_ITEMS table is automatically inherited from the primary key/foreign key relationship that exists. This time. defining ORDER_DATE in the ORDER_ITEMS table is redundant because there is a primary key/foreign key relationship between the two tables. When used for pruning and partitionwise joins. The right part of the graphic shows you the situation where you use reference partitioning.

quantity NUMBER(8). CONSTRAINT orders_pk PRIMARY KEY(order_id) ) PARTITION BY RANGE(order_date) (PARTITION Q105 VALUES LESS THAN (TO_DATE('1-4-2005'. It is created with four partitions. Copyright © 2007.Page 16 . Q205. As with other partitioned tables.'DD-MM-YYYY')). In this case. All rights reserved. The partitions of a reference-partitioned table can be named unless there is a conflict with inherited names. order_mode VARCHAR2(8). • It is not possible to disable the foreign key constraint of a reference-partitioned table. and Q405. ORDER_ITEMS: Reference-partitioned child table: • This table is created with four partitions—Q105. sales_rep_id NUMBER(6) . order_status NUMBER(2) . CONSTRAINT order_items_fk FOREIGN KEY(order_id) REFERENCES orders(order_id) ) PARTITION BY REFERENCE(order_items_fk). CREATE TABLE order_items ( order_id NUMBER(12) NOT NULL. • If partition descriptors are provided. The example above creates the following two tables: ORDERS: Range-partitioned table partitioned on order_date. order_date DATE.'DD-MM-YYYY')).Reference Partitioning: Example Reference Partitioning: Example CREATE TABLE orders ( order_id NUMBER(12) . customer_id NUMBER(6). the partition will have a system-generated name.'DD-MM-YYYY'))). and Q405—with each containing rows corresponding to ORDERS in the respective parent partition. Partitioning and Storage-Related Enhancements Chapter 1 . the number of partitions described must be exactly equal to the number of partitions or subpartitions in the referenced table. • Partitions of a reference-partitioned table will collocate with the corresponding partition of the parent table. promotion_id NUMBER(6). you can specify object-level default attributes. Q105. order_total NUMBER(8.2). and partition descriptors that override object-level defaults. • Partition bounds cannot be specified for the partitions of a reference-partitioned table.'DD-MM-YYYY')). PARTITION Q405 VALUES LESS THAN (TO_DATE('1-1-2006'. Q305. Q205. line_item_id NUMBER(3) NOT NULL. PARTITION Q305 VALUES LESS THAN (TO_DATE('1-10-2005'. unit_price NUMBER(8.2). PARTITION Q205 VALUES LESS THAN (TO_DATE('1-7-2005'. • If the parent table is a composite-partitioned table. Q305. product_id NUMBER(6) NOT NULL. Oracle. if no explicit tablespace is specified. then the table will have one partition for each subpartition of its parent.

All rights reserved.Page 17 . performing partition maintenance operations on the parent table is automatically cascaded to the child table.• It is not permitted to add or drop partitions of a reference-partitioned table. Copyright © 2007. However. Oracle. Partitioning and Storage-Related Enhancements Chapter 1 .

interval-range. and interval-hash are now supported composite partitioning methods. Range-Range Partitioning Composite range-range partitioning enables logical range partitioning along two dimensions. With the introduction of interval partitioning. Partitioning and Storage-Related Enhancements Chapter 1 . for example. for example. LIST. range partition by order_date and range subpartition by shipping_date. and range-range composite methods. list-hash. the only composite partitioning methods supported were range-list and range-hash. List-List Partitioning Composite list-list partitioning enables logical list partitioning along two dimensions. INTERVAL SP1 SP1 SP1 • List top level – List-List – List-Hash – List-Range … … … … SP1 SP1 SP2 SP2 SP2 SP2 SP2 • Interval top level – Interval-Range – Interval-List – Interval-Hash SP3 SP3 SP3 SP3 SP3 SP4 SP4 SP4 SP4 SP4 LIST. List-Range Partitioning Composite list-range partitioning enables logical range subpartitioning within a given list partitioning strategy. list partition by country_id and list subpartition by sales_channel. list partition by country_id and range subpartition by order_date. Oracle. HASH Before the release of Oracle Database 11g.Composite Partitioning Enhancements Composite Partitioning Enhancements • Range top level – Range-Range RANGE. With this new release. List-Hash Partitioning Composite list-hash partitioning enables hash subpartitioning of a list-partitioned object. All rights reserved. for example. list partitioning can be a top-level partitioning method for composite partitioned tables giving us list-list.Page 18 . RANGE. for example. list-range. Copyright © 2007. to enable partitionwise joins. interval-list.

Page 19 . SUBPARTITION sp2 VALUES LESS THAN (100000). the SALES table is created and range-partitioned on time_id. PARTITION VALUES LESS THAN (TO_DATE('1-8-2007'. PARTITION VALUES LESS THAN (TO_DATE('1-7-2007'. channel_id char(1) NOT NULL. quantity_sold NUMBER(3) NOT NULL. a single default partition bound by MAXVALUE (range) or DEFAULT value (list) is created.'DD-MM-YYYY')) ). All of the composite partitioning methods fully support the existing partition pruning methods for queries involving predicates on the subpartitioning key. If no template is specified. amount_sold NUMBER(10. Although the example illustrates the range-range methodology. Because of the template. Using a subpartition template. the SALES table is subpartitioned by range using cust_id as the subpartition key.'DD-MM-YYYY')). SUBPARTITION sp3 VALUES LESS THAN (150000).'DD-MM-YYYY')).Range-Range Partitioning: Example Range-Range Partitioning: Example CREATE TABLE sales ( prod_id NUMBER(6) NOT NULL.'DD-MM-YYYY')).2) NOT NULL ) PARTITION BY RANGE (time_id) SUBPARTITION BY RANGE (cust_id) SUBPARTITION TEMPLATE ( SUBPARTITION sp1 VALUES LESS THAN (50000). promo_id NUMBER (6) NOT NULL. In the example in the slide. the other new composite partitioning methods use similar syntax and statement structure. Oracle. all partitions have the same number of subpartitions with the same bounds as defined by the template. Composite range-range partitioning enables logical range partitioning along two dimensions. Partitioning and Storage-Related Enhancements Chapter 1 . PARTITION VALUES LESS THAN (TO_DATE('1-1-2008'. time_id DATE NOT NULL. cust_id NUMBER NOT NULL. Copyright © 2007. SUBPARTITION sp4 VALUES LESS THAN (MAXVALUE) ) ( PARTITION VALUES LESS THAN (TO_DATE('1-4-2007'. All rights reserved.

or Create Table As Select (CTAS). The Oracle database was the pioneer in terms of compression technology for databases with the introduction of table compression for bulk load operations in Oracle9i. Oracle technology reads the compressed data (less fetches needed) directly and does not require any uncompress operation. Partitioning and Storage-Related Enhancements Chapter 1 . – Batched compression ensures no impact for most OLTP transactions. Oracle Database 11g extends the compression technology to support these operations as well. Siebel. – Reads may actually see improved performance due to fewer I/Os and enhanced memory efficiency. – Support for conventional DML operations (INSERT. compression in Oracle Database 11g can be used for all kinds of workload. compression was not available for regular data manipulation operations such as INSERT. but also your read performance or queries improve. Consequently. Copyright © 2007. compression technology in Oracle Database 11g is very efficient and could reduce the space consumption by 50–75%. not only your write performance does not degrade. An enormous amount of work has gone into making sure that the new compression technology has negligible impact on updates because any noticeable write-time penalty due to compression will not be acceptable in an OLTP environment. Note: Compression technology is completely application transparent. and so on. UPDATE. Oracle.Page 20 . All rights reserved.Table Compression: Overview Table Compression: Overview • Oracle Database 11g extends compression for OLTP data. you could compress data at the time of performing bulk load using operations such as direct loads. until now. be it online transaction processing (OLTP) or data warehousing. EBS. Using this feature. However. As a result. This means that you can use this technology with any homegrown or packaged application such as SAP. This is because unlike desktopbased compression techniques where you have to wait for data to be uncompressed. It is important to mention that table compression enhancements introduced in Oracle Database 11g are not just incremental changes. and DELETE. • There is no impact on reads. And while you do that. UPDATE. DELETE) • New algorithm significantly reduces write overhead.

Page 21 . At that point compression is triggered again to reduce space occupation in the block. compression triggered Header PCTFREE limit Free space Inserts are uncompressed. Oracle.Table Compression Concepts Table Compression Concepts Inserts are again uncompressed. Copyright © 2007. data is stored in an uncompressed format (like for uncompressed tables). Compressed data Uncompressed data PCTFREE reached. potentially reducing the space it originally occupied. The slide shows you a data block evolution when that block is part of a compressed table. At the start. Partitioning and Storage-Related Enhancements Chapter 1 . All rights reserved. the block is empty and available for inserts. However. the data is automatically compressed. You should read it from left to right. This allows for new uncompressed inserts to take place in the same block. compression triggered Data block PCTFREE reached. When you start inserting into this block. until PCTFREE is reached again. and maximizes contiguous free space in blocks. as soon as you reach the PCTFREE of that block. Note: Compression eliminates holes created due to deletions.

– Does not trigger compression on existing rows To use the new compression algorithm. Copyright © 2007. you must flag your table with the COMPRESS FOR ALL OPERATIONS clause.Page 22 . You can do so at table creation. Oracle. If you use the COMPRESS clause without specifying any FOR option. or if you use the COMPRESS FOR DIRECT_LOAD OPERATIONS clause. Partitioning and Storage-Related Enhancements Chapter 1 . • Enable compression on existing table: ALTER TABLE t2 COMPRESS FOR ALL OPERATIONS.Using Table Compression Using Table Compression • Requires database compatibility level at 11. This is illustrated in the examples given above. Note: You can view compression flags for your tables using the COMPRESS and COMPRESS_FOR columns in views such as DBA_TABLES and DBA_TAB_PARTITIONS. You can also enable compression at the partition or tablespace level. you can use the DEFAULT storage clause of the CREATE TABLESPACE command to optionally specify a COMPRESS FOR clause. All rights reserved.1 or greater • New syntax extends the COMPRESS keyword: – COMPRESS [FOR {ALL | DIRECT_LOAD} OPERATIONS] – FOR DIRECT_LOAD is the default: Refers to bulk load operations from prior releases – FOR ALL OPERATIONS: OLTP + direct loads • Enable compression for new tables: CREATE TABLE t1 COMPRESS FOR ALL OPERATIONS. For example. you fall back to the old compression mechanism that was available in earlier releases. or after creation.

you should have learned how to: • Implement the new partitioning methods • Employ data compression Copyright © 2007. Oracle. Partitioning and Storage-Related Enhancements Chapter 1 .Summary Summary In this lesson. All rights reserved.Page 23 .

Copyright © 2007.Page 24 . Partitioning and Storage-Related Enhancements Chapter 1 . Oracle. All rights reserved.

Using the New and Updated Materialized Views (MV) Catalog Views Copyright © 2007. Oracle. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 0 . All rights reserved.Page 1 .

Page 2 . Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 .Chapter 2Using the New and Updated Materialized Views (MV) Catalog Views Using the New and Updated Materialized Views (MV) Catalog Views Copyright © 2007. All rights reserved. Oracle.

Oracle.Page 3 .Objectives Objectives After completing this lesson. you should be able to: • Identify the benefits of the new and updated MV catalog views • List the new and updated MV catalog views • Describe the new and updated columns in the MV catalog views • Identify the refresh performance enhancements Copyright © 2007. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . All rights reserved.

1) documentation.Page 4 . see the Oracle Database 10g: Implement and Administer a Data Warehouse Instructor-Led Training or the Oracle Database Data Warehousing Guide 11g Release 1 (11. Note: For additional information about materialized views and partition change tracking.The New and Updated MV Catalog Views The New and Updated MV Catalog Views • New catalog views display the partition change tracking (PCT) information for a given materialized view. Copyright © 2007. • It affects the usability and maintainability of the materialized view. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . • This is a critical piece of information for the user to be able to view the partition staleness information of the materialized view. • The new catalog views display which sections of the materialized views data are fresh or stale. All rights reserved. Oracle.

and to show numbers of fresh and stale PCT partitions. which could help them make better decisions as to how their materialized views should be maintained. The fresh part of MV is reliable and available for use. The USER/ALL/DBA_MVIEWS extension describes all materialized views in the database. number of fresh and stale PCT regions. Create a new catalog view for PCT subpartition (USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION) to describe the freshness of each PCT subpartition. users are unable to see this valuable information.Page 5 . A new catalog view for PCT partition (USER/ALL/DBA_MVIEW_DETAIL_PARTITION) is created to describe the freshness of each PCT partition. This provides users with the information regarding which ranges of the MV data are fresh and which are not. All rights reserved. Oracle Database 11g exposes the MV freshness information corresponding to PCT base table partitions. The USER/ALL/DBA_MVIEWS catalog view is extended where new columns are added to describe number of PCT tables. The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS extension represents the named detail Copyright © 2007. Oracle. The USER/ALL/DBA_DETAIL_RELATIONS catalog view is extended where new columns are added to indicate if the detail table is PCT-enabled. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . Therefore.PCT Catalog Views Showing Staleness Corresponding to Base Partitions PCT Catalog Views Showing Staleness Corresponding to Base Partitions USER/ALL/DBA_MVIEWS views (UPDATED) USER/ALL/DBA_MVIEW_DETAIL _RELATIONS views (UPDATED) USER/ALL/DBA_MVIEW_DETAIL_ PARTITION views (NEW) USER/ALL/DBA_MVIEW_DETAIL_ SUBPARTITION views (NEW) Partition Change Tracking (PCT) was introduced in Oracle Database 9i but its related information is not yet exposed to the user through catalog views.

Page 6 . with respect to a PCT detail subpartition. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . The new USER/ALL/DBA_MVIEW_DETAIL_PARTITION displays the freshness information of the materialized views. The new USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION displays freshness information for all materialized views in the database. Oracle. with respect to a PCT detail partition. All rights reserved.relations that are either in the FROM list of a materialized view or that are indirectly referenced through views in the FROM list. Copyright © 2007.

. Oracle. . two extended columns show how many fresh and stale PCT regions are present in that MV. The DBA_MVIEWS describes all materialized views in the database. Copyright © 2007. This catalog view is extended to show how many detail partitions support PCT. The USER_MVIEWS describes all materialized views owned by the current user. NUM_PCT_TABLES (NEW) NUM_FRESH_PCT_REGIONS (NEW) Column Description The name of the owner of the materialized view The name of the materialzed view . . The ALL_MVIEWS describes all materialized views accessible to the current user. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . All rights reserved. see the Oracle Database Reference 11g Release 1 (11.Page 7 .The New Columns in the USER/ALL/DBA_MVIEWS Catalog View The New Columns in the USER/ALL/DBA_MVIEWS Catalog View Catalog View Column OWNER MVIEW_NAME . Specifies the number of PCT detail tables Specifies the number of Fresh PCT partition regions NUM_STALE_PCT_REGIONS (NEW) Specifies the number of Stale PCT partition regions The syntax shown above is the extended specification for the DBA/ALL/USER_MVIEWS catalog view.1) guide. Note: For the complete columns listing for the DBA/ALL/USER_MVIEWS catalog views. . In addition.

VIEW. the catalog views display how many fresh and stale PCT partitions are present in that detail table. SNAPSHOT. If the detail partition does support PCT. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 .The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS Catalog View Extension The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS Catalog View Extension Catalog View Columns OWNER MVIEW_NAME DETAILOBJ_OWNER DETAILOBJ_NAME DETAILOBJ_TYPE DETAILOBJ_ALIAS DETAILOBJ_PCT (NEW) NUM_FRESH_PCT_PARTITIONS (NEW) DETAILOBJ_PCT (NEW) Column Description The name of the owner of the materialized view The name of the materialzed view The name of the owner of the detail object The detail object name (table or view) TABLE. All rights reserved.Page 8 . Copyright © 2007. or UNDEFINED Implicit or explicit alias for detail relation Is the detail object PCT supported? (Y or N) The number of Fresh PCT partitions The number of Stale PCT partition regions The DBA/ALL/USER_ MVIEW_DETAIL_RELATIONS catalog views are extended to show whether the detail partition supports PCT with respect to a given MV. The columns shown above in red are the new columns that were added to the DBA/ALL/USER_ MVIEW_DETAIL_RELATIONS catalog views. Inline views in the materialized view definition are not represented in this view or the related views. The ALL_MVIEW_DETAIL_RELATIONS describes the named detail relations that are either specified in the FROM list of the subquery that defines a materialized view accessible to the current user or that are indirectly referenced through views in that FROM list. Oracle. CONTAINER.

owned by the current user. and so on The new DBA/ALL/USER_MVIEW_DETAIL_RELATIONS catalog views shown above display the freshness information of the materialized views.The New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_PARTITION The New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_PARTITION Catalog View Columns OWNER MVIEW_NAME DETAILOBJ_OWNER DETAILOBJ_NAME DETAIL_PARTITION_NAME DETAIL_PARTITION_POSITION FRESHNESS Column Description The name of the owner of the materialized view The name of the materialzed view The name of the owner of the detail object The detail object name (table or view) The name of the detail object partition The position of the detail object partition The freshness state: FRESH. All rights reserved. The DBA_MVIEW_DETAIL_PARTITION displays freshness information for all materialized views in the database. STALE. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . Oracle. with respect to a PCT detail partition. with respect to a PCT detail partition. accessible to the current user. Copyright © 2007. with respect to a PCT detail partition. The USER_MVIEW_DETAIL_PARTITION displays freshness information for all materialized views.Page 9 . UNKNOWN. NA.

and so on The ALL_MVIEW_DETAIL_SUBPARTITION displays the freshness information of the materialized views. with respect to a PCT detail subpartition. Oracle.The New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION The New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION Catalog View Columns OWNER MVIEW_NAME DETAILOBJ_OWNER DETAILOBJ_NAME DETAIL_PARTITION_NAME DETAIL_SUBPARTITION_NAME DETAIL_SUBPARTITION_POSITION FRESHNESS Column Description The name of the owner of the materialized view The name of the materialzed view The name of the owner of the detail object The detail object name (table or view) The name of the detail object partition The name of the detail object subpartition The position of the detail object partition The freshness state: FRESH. Copyright © 2007. UNKNOWN. accessible to the current user. All rights reserved. owned by the current user. STALE. with respect to a PCT detail subpartition. NA. The USER_MVIEW_DETAIL_SUBPARTITION displays freshness information for all materialized views.Page 10 . The DBA_MVIEW_DETAIL_SUBPARTITION displays freshness information for all materialized views in the database. with respect to a PCT detail subpartition. Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 .

Page 11 . Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . an index is created automatically during MV creation. All rights reserved. Automatically Create Proper Index on MV (UNION ALL MV) Currently. we plan to address the issue for the atomic refresh so that when the materialized view is refreshed in the atomic mode. it is implicitly disabled for query rewrite even if its data is acceptable for the user. the user needs to manually create such an index. see the Oracle Database 10g: Copyright © 2007. it is eligible for query rewrite if the rewrite integrity mode is set to “STALE_TOLERATED”. PCT refresh is now allowed for UNION ALL MV fast refresh. the fast refresh was extended to support the MV that has UNION ALL operators. it is not created with an index. Query Rewrite Possible Using the Materialized View During Its Atomic Refresh Currently. Oracle. Using the PCT refresh can be beneficial during the refresh of UNION ALL MV. However. when the materialized view is being refreshed. In 11g. the fast refresh of the UNION ALL MV did not apply partition-based (PCT) refresh. This is especially true when the atomic refresh is in progress and the user will see the data in the materialized view in a transactional state of the past refresh. In Oracle Database 11g. the materialized view with UNION ALL operators is fast refreshable but unlike most of the fast refreshable MVs.Refresh Performance Improvements in Oracle Database 11g Refresh Performance Improvements in Oracle Database 11g Refresh operations on materialized views are now faster with the following improvements: • Refresh statement combinations (merge and delete) • Removal of unnecessary refresh hint • Index creation for UNION ALL MV • PCT refresh possible for UNION ALL MV The refresh performance improvements reduce the time required to refresh materialized views. Partition-Based Refresh for UNION ALL MVs In Oracle Database 9i Release 2. Note: For information about refreshing materialized views. In Oracle Database 11g. To speed up the refresh execution.

All rights reserved.Implement and Administer a Data Warehouse Instructor-Led Training or the Oracle Database Data Warehousing Guide 11g Release 1 (11.1) documentation.Page 12 . Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . Copyright © 2007. Oracle.

Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . you should have learned how to: • Identify the benefits of the new and updated MV catalog views • List the new and updated MV catalog views • Describe the new and updated columns in the MV catalog views • Identify the refresh performance enhancements Copyright © 2007.Page 13 . All rights reserved. Oracle.Summary Summary In this lesson.

Page 14 . Using the New and Updated Materialized Views (MV) Catalog Views Chapter 2 . All rights reserved.Copyright © 2007. Oracle.

General Query Rewrite Enhancements Copyright © 2007. All rights reserved. General Query Rewrite Enhancements Chapter 0 .Page 1 . Oracle.

All rights reserved. General Query Rewrite Enhancements Chapter 3 .Page 2 . Oracle.Chapter 3General Query Rewrite Enhancements General Query Rewrite Enhancements Copyright © 2007.

Oracle.Page 3 .Objectives Objectives After completing this lesson. you should be able to: • Review the benefits of using materialized views • Review the benefits of query rewrite • Use the query rewrite enhancement to support queries with inline views • Identify the query rewrite enhancement that supports queries with remote tables Copyright © 2007. General Query Rewrite Enhancements Chapter 3 . All rights reserved.

Note: The term summary comes from the fact that most of the time users in data warehouse Copyright © 2007. Instead of precomputing the same query result many times. The DBA creates materialized views that are automatically used by the system when rewriting SQL queries.Page 4 . With Oracle Database summary management. Although this approach has the benefit of enhancing query response time. Many data warehouses are also using a technique called summaries. which is comparable to a CREATE TABLE AS SELECT (CTAS) statement. in that the data can be refreshed automatically. All rights reserved. When you use a summary. the end user no longer has to be aware of the summaries that have been defined. Oracle. General Query Rewrite Enhancements Chapter 3 . summaries are implemented with a materialized view. The basic process for a summary is to precompute the result of a long-running query and store this result in a database table called a summary table. the results of joins and aggregate operations are calculated before query execution and stored in a database table. it also has many drawbacks. or you can partition your data. the user can directly access the summary table. There are many well-known techniques you can use to increase query performance. and must be manually refreshed whenever modifications occur on the real tables. you can create additional indexes. the data contained in a summary table is frozen. Also. The user needs to be aware of the summary table’s existence in order to rewrite the query to use that table instead.Using Summaries to Improve Performance Using Summaries to Improve Performance • Special types of aggregate views • Improved query execution time through precomputing of expensive joins and aggregation operations before execution and storing of results in a database table • Created using a schema object called a materialized view Summaries are aggregate views that are created to improve query execution times. In an Oracle database. Using materialized views offers another advantage over manually creating summaries tables. For example.

All rights reserved.environments are computing expensive joins with aggregations. When creating summaries using materialized views in the Oracle Database. you are not required to use joins or aggregations. Copyright © 2007.Page 5 . General Query Rewrite Enhancements Chapter 3 . Oracle.

Oracle server rewrites SQL query to use summary tables. General Query Rewrite Enhancements Chapter 3 .Page 6 . The use of the materialized view is transparent to the end user or application querying the data. When the end user queries tables and views. Oracle. the Oracle server query rewrite mechanism automatically rewrites the SQL query to use the summary table. In a typical use of summary management. Copyright © 2007.Summary Management Summary Management DBA creates materialized view (summary table). End user queries tables and views. the database administrator creates the materialized view (summary table). All rights reserved.

General Query Rewrite Enhancements Chapter 3 . Oracle.Page 7 . All rights reserved. Copyright © 2007.Summary Management Components Summary Management Components • Mechanisms to define materialized views and dimensions • Refresh mechanism to ensure materialized views contain the latest data • Query rewrite capability to transparently rewrite a query to use a materialized view • SQL Access Advisor: Recommends materialized views and indexes to be created • DBMS_ADVISOR.TUNE_MVIEW procedure: Shows you how to make your materialized view fast refreshable and use general query rewrite The implementation of summary management in Oracle Database includes the use of the components listed above.

Using Summary Management Using Summary Management 1. General Query Rewrite Enhancements Chapter 3 . Use the SQL Access Advisor to determine how you will use materialized views. Use DBMS_ADVISOR. 3. and loaded into the detail tables. After your data has been transformed. you invoke the summary management process as described above. All rights reserved. staged.TUNE_MVIEW to obtain an optimized materialized view as necessary.Page 8 . Oracle. 2. Create materialized views and design how queries will be rewritten. Copyright © 2007.

General Query Rewrite Enhancements Chapter 3 .Page 9 .Query Rewrite: Overview Query Rewrite: Overview • Tries to use materialized views instead of base tables to return query results • Can save orders of magnitude of CPU and elapsed time to return results because queries are precomputed • Queries rewritten even if not in the exact form of the MV • Various requirements for query rewrite to take place Copyright © 2007. Oracle. All rights reserved.

it selects the one that can result in reading the least amount of data. Query rewrite is attempted recursively to take advantage of nested materialized views. the optimizer performs the rewrite and then tests whether the rewritten query can be rewritten further with another materialized view (this could be the case only when nested materialized views exist). Generate plan Compare plan costs Pick best plan Query rewrite is available only with the cost-based optimizer. Oracle optimizes the input query with and without rewrite and selects the least costly alternative. one at a time. Copyright © 2007.Cost-Based Query Rewrite Process Cost-Based Query Rewrite Process Query rewrite Generate plan End user queries tables and views. This process continues until no further rewrites are possible. If the rewrite logic has a choice between multiple materialized views to rewrite a query block. General Query Rewrite Enhancements Chapter 3 . After a materialized view has been picked for a rewrite. The optimizer rewrites a query by rewriting one or more query blocks. Oracle.Page 10 . All rights reserved.

Copyright © 2007. All rights reserved.Page 11 . It also operates on subqueries in set operators: UNION. INTERSECT. and MINUS. General Query Rewrite Enhancements Chapter 3 . DELETE.What Can Be Rewritten? What Can Be Rewritten? • Queries and subqueries in the following types of SQL statements: – SELECT – CREATE TABLE … AS SELECT – INSERT INTO … SELECT • Subqueries in DML statements: — — — INSERT UPDATE DELETE UNION UNION ALL INTERSECT MINUS • Subqueries in the set operators: — — — — Query rewrite operates on queries and subqueries in the following types of SQL statements: • SELECT • CREATE TABLE … AS SELECT • INSERT INTO … SELECT Query rewrite operates on subqueries in DML statements: INSERT. and UPDATE. UNION ALL. Oracle.

The query rewrite in Oracle Database 11g supports queries containing inline views. and general rewrite processing was possible.Query Rewrite Enhancement to Support Queries Containing Inline Views Query Rewrite Enhancement to Support Queries Containing Inline Views Yes Query with inline view Query inline view’s (IV) text matches the MV’s IV text? No Rewrite query No Yes No query rewrite Query inline view’s (IV) text equivalent to the MV’s IV text? Oracle Database 10g supported general query rewrite when the user query contained an inline view. General Query Rewrite Enhancements Chapter 3 . More queries are now eligible for query rewrite thus improving system throughput and performance. or • When the request query contains inline views that are equivalent to the inline views in the materialized view Copyright © 2007. All rights reserved. Oracle. Oracle Database 10g treated the matching inline view as it would a named view. or a subquery in the FROM list. Query rewrite matched inline views in the materialized view with inline views in the request query when the text of the two inline views exactly match.Page 12 . In that case. Oracle Database 11g supports query rewrite with inline views in two circumstances: • When the text from the inline views in the materialized view exactly matches the text in the request query.

General Query Rewrite Enhancements Chapter 3 .When Are Two Inline Views Equivalent? When Are Two Inline Views Equivalent? Two inline views are considered equivalent if: • The SELECT lists and GROUP BY lists are equivalent • The FROM clauses contain the same or equivalent objects • The join graphs. All rights reserved.Page 13 . Oracle. including all the selections in the WHERE clauses are equivalent • The HAVING clauses are equivalent Copyright © 2007.

prod_id. Oracle.prod_id = products.prod_id.prod. mv_iv.cust_id.Page 14 . the query inline view will be internally replaced with the materialized view's inline view so that the query can be rewritten.prod_id = products. therefore.prod_id. -. products WHERE sales. General Query Rewrite Enhancements Chapter 3 . All rights reserved. displays a materialized view that contains an inline view. sum(mv_iv.prod_id) MV_IV GROUP BY mv_iv. therefore.prod_id. The first example shown above.amount_sold) sum_amount_sold FROM (SELECT products.prod_id.amount_sold) sum_amount_sold FROM (SELECT products.cust_id. cust_id. Copyright © 2007.cust_id.MV.id) IV GROUP BY iv. SUM(iv. amount_sold FROM sales.An MV Inline View Text Matches a Query’s Inline View Text: Example An MV Inline View Text Matches a Query’s Inline View Text: Example CREATE MATERIALIZED VIEW SUM_SALES_MV ENABLE QUERY REWRITE AS SELECT mv_iv. The query in the second example shown above has an inline view whose text matches exactly with that of the materialized view's inline view. cust_id. products WHERE sales. the query is rewritten with the MV SELECT iv.The text of the IV matches exactly the text of the -. amount_sold FROM sales. mv_iv. iv.prod_id. iv.cust_id.

displays a materialized view that contains an inline view. cust_id.prod_id. sales WHERE sales. iv. The first example shown above has an inline view that does not have exact text match with the inline view in the preceding materialized view.however.cust_id. Note that the join predicate in the query inline view is switched. Note that their texts do not match. displays a query that has an equivalent inline view to the inline view found in the materialized view example above.prod_id.prod_id. It gets the same object number and rewrite takes place. Even though this query does not textually match with that of the materialized view's inline view.prod_id. products WHERE sales. amount_sold FROM products.prod_id) IV GROUP BY iv.cust_id.amount_sold) sum_amount_sold FROM (SELECT products. mv_iv. they are equivalent SELECT iv.prod_id) MV_IV GROUP BY mv_iv.prod_id = products. Copyright © 2007.prod_id. query rewrite will identify the query's inline view as equivalent to the materialized view's inline view.The text of the IV doesn’t match the text of the MV. cust_id. -. All rights reserved. As before. amount_sold FROM sales. the query inline view will be internally replaced with the materialized view's inline view so that the query can be rewritten. SUM(iv. mv_iv.cust_id. -.cust_id.Page 15 . The first example shown above. Both of the two earlier queries are rewritten with SUM_SALES_MV as shown in the code example above. General Query Rewrite Enhancements Chapter 3 . The second example shown above. sum(mv_iv.amount_sold) sum_amount_sold FROM (SELECT products. iv. Oracle.prod_id = products.An MV Inline View Text Is Equivalent to a Query’s Inline View Text: Example An MV Inline View Text Is Equivalent to a Query’s Inline View Text: Example CREATE MATERIALIZED VIEW SUM_SALES_MV ENABLE QUERY REWRITE AS SELECT mv_iv.prod_id.

Page 16 .cust_id. sales WHERE sales.Both above queries are finally re-written as follows: SELECT p_id.prod_id) IV GROUP BY iv. sum_sold FROM SUM_SALES_MV.prod_id.Both above queries are first transformed as follows: SELECT prod_id. Next the query inline view for both examples will be internally replaced with the materialized view's inline view so that the query can be rewritten.prod_id. iv.cust_id. cust_id.prod_id. Oracle.prod_id) IV GROUP BY iv.prod_id.cust_id. -.Transforming and Rewriting the Query from the Two Previous Examples Transforming and Rewriting the Query from the Two Previous Examples SELECT iv. -.cust_id. cust_id. Both previous query examples with the matching and equivalent inline view texts to that of the MV are first transformed as shown in the second example above to use the MV. amount_sold FROM products. amount_sold FROM sales.amount_sold) sum_amount_sold FROM (SELECT products. General Query Rewrite Enhancements Chapter 3 .prod_id. SUM(iv.prod_id = products. SELECT iv.prod_id. SUM(iv. sum(amount_sold) FROM MV_IV GROUP BY prod_id. iv.prod_id = products. products WHERE sales. cust_id. Copyright © 2007. iv. All rights reserved. iv. cust_id. c_id.amount_sold) sum_amount_sold FROM (SELECT products.

Oracle. General Query Rewrite Enhancements Chapter 3 . All rights reserved.Page 17 .Inline View Rewrite Guidelines Inline View Rewrite Guidelines General inline view rewrite is not supported for queries that contain the following: • Set operators • GROUPING SET clauses • Nested subqueries • Nested inline views • Remote tables Copyright © 2007.

if the query runs faster.EXPLAIN_REWRITE procedure. rewrite should have occurred.Page 18 .Did Query Rewrite Occur? Did Query Rewrite Occur? Use the EXPLAIN PLAN statement. Was the query rewritten? Use the DBMS_MVIEW. Execute the query. Note: For additional information about verifying whether or not the query was rewritten using the EXPLAIN PLAN statement and the DBMS_MVIEW. General Query Rewrite Enhancements Chapter 3 .EXPLAIN_REWRITE procedure to see whether a query will be rewritten or not. it is not always evident that it has taken place.1) documentation. All rights reserved.EXPLAIN_ REWRITE procedure. There are two ways to confirm that the query rewrite has occurred: • Use the EXPLAIN PLAN statement and check whether the OBJECT_NAME column contains the name of a materialized view. Copyright © 2007. nor can it be dumped in a trace file. Because query rewrite occurs transparently. but there is no proof to confirm this. see the Oracle Database 10g: Implement and Administer a Data Warehouse Instructor-Led Training or the Oracle Database Data Warehousing Guide 11g Release 1 (11. • Use the DBMS_MVIEW. Of course. The rewritten statement is not stored in the V$SQL view. Oracle.

Note: Because the constraint information of the remote tables is not available at the remote site. • Whenever a query contains columns that are not found in the MV. This reduces or eliminates the data from network (network round trips). a join back is used to rewrite the query. Oracle. Copyright © 2007. • The MV should be present at the site where the query is being issued.Page 19 . which is a costly operation. Query rewrite can reference remote objects using MVs that reference the remote objects. General Query Rewrite Enhancements Chapter 3 . All rights reserved. • If the join back table is not found at the local site. • Because any remote table update cannot be propagated to the local site simultaneously.Query Rewrite Using Remote Tables in Oracle Database 11g Query Rewrite Using Remote Tables in Oracle Database 11g • Oracle supports query rewrite with MVs that reference tables at a single remote database site. query rewrite will only work in the stale_tolerated mode. query rewrite will not make use of any constraint information. query rewrite will not take place. Oracle Database 11g extends the current query rewrite capability.

prod_id.amount_sold) AS sum_amount_sold FROM sales@remotedbl s.cust_id.week_ending_day. it will be rewritten using the above materialized view as shown in the third example above.Page 20 . cust_id.time_id AND s.time_id=t.cust_id. times@remotedbl t WHERE s.prod_id=p. t.week_ending_day. t.prod_id. but it references tables that are all found at the remote site. Oracle. times@remotedbl t WHERE s.prod_id. The query in the second example shown above contains tables that are found at a single remote site. week_ending_day.Query Rewrite Using Remote Tables: Example Query Rewrite Using Remote Tables: Example CREATE MATERIALIZED VIEW sum_sales_prod_week_mv ENABLE QUERY REWRITE AS SELECT p. SELECT prod_id. s. All rights reserved.amount_sold) AS sum_amount_sold FROM sales@remotedbl s. s. SELECT p. Even though the query references remote tables. s.time_id AND s.week_ending_day. products@remotedbl p. SUM(s. General Query Rewrite Enhancements Chapter 3 . s.products@remotedbl p.time_id=t.prod_id GROUP BY p. sum_amount_sold The materialized view shown in the first example above is present at the local site.cust_id. SUM(s.cust_id. t.week_ending_day. Copyright © 2007.prod_id GROUP BY p.prod_id=p. t. FROM sum_sales_prod_week_mv.prod_id.

Summary Summary In this lesson. General Query Rewrite Enhancements Chapter 3 . All rights reserved. you should have learned how to: • Review the benefits of using materialized views • Review the benefits of query rewrite • Use the query rewrite enhancement to support queries with inline views • Identify the query rewrite enhancement that supports queries with remote tables Copyright © 2007.Page 21 . Oracle.

Page 22 .Copyright © 2007. General Query Rewrite Enhancements Chapter 3 . Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 0 . All rights reserved. Oracle.Page 1 .Performing Pivoting and Unpivoting Operations Copyright © 2007.

Oracle.Chapter 4Performing Pivoting and Unpivoting Operations Performing Pivoting and Unpivoting Operations Copyright © 2007.Page 2 . All rights reserved. Performing Pivoting and Unpivoting Operations Chapter 4 .

Oracle.Page 3 . Performing Pivoting and Unpivoting Operations Chapter 4 . All rights reserved. you should be able to: • Identify the benefits of pivoting and unpivoting operations • Write crosstab queries to pivot (rotate) column values into new columns and to unpivot (rotate) columns into column values • Pivot and unpivot with multiple columns and multiple aggregates • Use wildcards and aliases with pivoting operations Copyright © 2007.Objectives Objectives After completing this lesson.

including computer hardware and software. the supplier. Copyright © 2007. All rights reserved. a minimum price at which a product will be sold. and a URL for manufacturer information. Performing Pivoting and Unpivoting Operations Chapter 4 . and tools. The company sells several categories of products. a list price. music. clothing. the warranty period if applicable. Oracle.The Order Entry (OE) Schema Used in This Lesson The Order Entry (OE) Schema Used in This Lesson CUSTOMERS ORDER_ITEM order_id line_item_id product_id unit_price quantity ORDERS order_id order_date order_mode customer_id order_status order_total sales_rep_id promotion_id customer_id cust_first_name cust_ last_name cust_ address_typ street_address postal_code city state_province country_id phone_numbers nls_language nls_territory credit_limit cust_ email account_mgr_id date_of_birth marital_status gender Income_level PRODUCT_ INFORMATION product_id product_name product_description category_id weight_class warranty_period supplier_id product_status list_price min_price catalog_url PRODUCT_ DESCRIPTION product_id language_id product_name product_description INVENTORIES product_id warehouse_id quantity_on_hand WAREHOUSES warehouse_id warehouse_name location_id The ER-diagram for the OE schema is displayed above and on the following pages to get you familiar with the names of tables and columns that are used throughout this lesson. the weight group (for shipping purposes).Page 4 . the status of the product. the category into which the product falls. The company maintains product information that includes product identification numbers.

The Sales History (SH) Schema Used in This Lesson

The Sales History (SH) Schema Used in This Lesson
PROMOTIONS
promo_id promo_name promo_subcategory promo_subcategory_id promo_category promo_category_id promo_cost promo_begin_date promo_end_date promo_total promo_total_id

TIMES
time_id day_name day_number_in_week day_number_in_month calendar_week_number fiscal_week_number week_ending_day week_ending_day_id calendar_month_number fiscal_month_number calendar_month_desc calendar_month_id fiscal_month_id days_in_cal_month days_in_fis_month end_of_cal_ month end_of_fis_month calendar _month _name fiscal _month _name calendar _quarter _desc calendar_quarter_id fiscal _quarter _desc fiscal _quarter _id days_in_cal_quarter days_in_fis_quarter end_of_cal_quarter end_of_fis_quarter calendar_quarter_number fiscal_quarter_number calendar_year calendar_year_id fiscal_year fiscal_year_id days_in_cal_year days_in_fis_year end_of_cal_year end_of_fis_year

COSTS
prod_id time_id promo_id channel_id unit_cost unit_price

SALES
prod_id cust_id time_id channel_id promo_id quantity_sold amount_sold

CHANNELS
channel_id channel_desc channel_class channel_class_id channel_total channel_total_id

PRODUCTS

The company does a high volume of business, so it runs business statistics reports to aid in decision support. Many of these reports are time based and nonvolatile. That is, they analyze past data trends. The company loads data into its data warehouse regularly to gather statistics for the reports. The reports include annual, quarterly, monthly, and weekly sales figures by product. The company also runs reports on the distribution channels through which its sales are delivered. When the company runs special promotions on its products, it analyzes the impact of the promotions on sales. It also analyses sales by geographical area.

Copyright © 2007, Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 4 - Page 5

The Sales History (SH) Schema Used in this eStudy

The Sales History (SH) Schema Used in this eStudy
COSTS SALES SALES

PRODUCTS
prod_id prod_name prod_desc prod_subcategory prod_subcategory_id prod_subcategory_desc prod_category prod_category_id prod_category_desc prod_weight_class prod_unit_of_measure prod_pack_size supplier_id prod_status prod_list_price prod_min_price prod_total prod_total_id prod_src_id prod_eff_from prod_eff_to prod_valid

CUSTOMERS
cust_id cust_first_name cust_last_name cust_gender cust_year_of_birth cust_marital_status cust_street_address cust_postal_code cust_city cust_city_id cust_state_province cust_state_province_id country_id cust_main_phone_number cust_income_level cust_credit_limit cust_email cust_total cust_total_id cust_src_id cust_eff_from cust_eff_to cust_valid

COUNTRIES
country_id country_iso_code country_name country_subregion country_subregion_id country_region country_region_id country_total country_total_id Country_name_hist

Copyright © 2007, Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 4 - Page 6

The Benefits of Using Pivoting Operations

The Benefits of Using Pivoting Operations

• Data returned by business intelligence (BI) queries is more useful if presented in a cross-tabular format. • Pivoting enables you to transform multiple rows of input into fewer rows, generally with more columns. • When pivoting, an aggregation operator is applied, enabling the query to condense large data sets into smaller more readable results. • Performing pivots on the server side can:
– Enhance processing speed – Reduce network load

Pivoting is a key technique in BI queries where you transform multiple rows of input into fewer rows, generally with more columns. By performing pivots on the server side: • Processing burden is removed from client applications, simplifying client-side development and potentially enhancing processing speed • Network load is reduced, because only aggregated pivot results need to traverse the network and not the detail data Data that was originally on multiple rows can be transformed into a single row of output, enabling intrarow calculations without a SQL Join operation.

Copyright © 2007, Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 4 - Page 7

it rotates data from columns into rows. aggregating data in the process. Performing Pivoting and Unpivoting Operations Chapter 4 . • You can use the UNPIVOT operator of the SELECT statement to rotate columns into values of a column. instead. Oracle.Page 8 . Copyright © 2007. PIVOT UNPIVOT Note: An UNPIVOT does not reverse a PIVOT operation. All rights reserved.The PIVOT and UNPIVOT Clauses of the SELECT Statement The PIVOT and UNPIVOT Clauses of the SELECT Statement • You can use the PIVOT operator of the SELECT statement to write cross-tabular queries that rotate the column values into new columns.

Pivoting on the QUARTER Column: Conceptual Example

Pivoting on the QUARTER Column: Conceptual Example
PRODUCT Shorts Shorts Shorts Kids Jeans Kids Jeans Kids Jeans COUNTRY Germany Poland USA Japan USA USA CHANNEL C P S C I I QUARTER Q2 Q2 Q3 Q2 Q1 Q4 AMOUNT_ SOLD 20,000 40,000 30,000 60,000 40,000 30,000 QUANTITY_ SOLD 1,000 2,500 2,000 2,000 2,500 1,500

PRODUCT Shorts Kids Jeans

Q1

Q2 3,500 2,500 2,000

Q3 2,000

Q4

1,500

The first example above displays the columns of a table. The table in the second example displays the results of pivoting on the QUARTER column. The values of the QUARTER column, Q1, Q2, Q3, and Q4 are rotated into new columns. The quantity sold is grouped by products and quarters. To accomplish the desired result, you can use the PIVOT clause to PIVOT the QUARTER column, that is, turn the values of this column into separate columns and aggregate data using a group function such as SUM on the QUANTITY_SOLD along the way for each PRODUCT. In the second example, the QUARTER column is pivoted. The values of the QUARTER column namely Q1, Q2, Q3, and Q4 are rotated into columns. In addition, the quantity sold is calculated for each quarter for ALL products, ALL channels, and ALL countries. For example, the total number of Shorts sold for all channels, all countries, for quarter 2 is 3500. The total number Kids Jeans sold for all channels, all countries, for quarter 2 is 2000. Note that the first example contains six rows before the pivoting operation. In the second example and after pivoting the QUARTER column, only two rows are displayed. Pivoting transforms multiple rows of input into fewer and generally wider rows.

Copyright © 2007, Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 4 - Page 9

The PIVOT Clause Syntax

The PIVOT Clause Syntax
table_reference PIVOT [ XML ] ( aggregate_function ( expr ) [[AS] alias ] [, aggregate_function ( expr ) [[AS] alias ] ]... pivot_for_clause pivot_in_clause ) -- Specify the column(s) to pivot whose values are to -- be pivoted into columns. pivot_for_clause = FOR { column |( column [, column]... ) }

-- Specify the pivot column values from the columns you -- specified in the pivot_for_clause. pivot_in_clause = IN ( { { { expr | ( expr [, expr]... ) } [ [ AS] alias] }... | subquery | { ANY | ANY [, ANY]...} } )

You can use the pivot clause to write cross-tabular queries that rotate rows into columns, aggregating the data in the process of the rotation. The XML keyword is required when you use either a subquery or the wildcard ANY in the pivot_in_clause to specify pivot values. You cannot specify XML when you specify explicit pivot values using expressions in the pivot_in_clause. If the XML keyword is used, the output will include grouping columns and one column of XMLType rather than a series of pivoted columns. The optional AS alias enables you to specify an alias for each measure. Note that the aggregate function has an implicit GROUP BY based on the columns in the source data. The aggregate_function operates on the table’s data, and the result of the computation appears in the crosstab report. The expr argument for the aggregate function is the measure to be pivoted. It must be a column or expression of the query_table_expression on which the pivot_clause is operating. The optional AS alias enables you to specify an alias for each measure. In the pivot_for_clause, specify one or more columns whose values are to be pivoted into columns. In the pivot_in_clause, specify the pivot column values from the columns you specified in the pivot_for_clause. For expr, specify a constant value of a pivot column. You can optionally provide an alias for each pivot column value. Use a subquery to extract the pivot column values by way of a nested subquery. If you specify ANY, then all values of the pivot columns are pivoted into columns. Subqueries and wildcards are useful if you do not know the specific values in the pivot columns.
Copyright © 2007, Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 4 - Page 10

However, you will need to do further processing to convert the XML output into a tabular format. The values evaluated by the pivot_in_clause become the columns in the pivoted data.

Copyright © 2007, Oracle. All rights reserved.

Performing Pivoting and Unpivoting Operations Chapter 4 - Page 11

customers. SUM(quantity_sold) AS quantity_sold FROM sales. country_name AS country. All rights reserved. countries.cust_id AND customers.2) AS quarter. sales_view. times. CALENDAR_QUARTER_DESC --------------------1999-02 2000-04 2002-03 2000-03 2001-04 1998-01 Copyright © 2007. channel_id AS channel. 2). 6.Creating a New View: Example Creating a New View: Example CREATE OR REPLACE VIEW sales_view AS SELECT prod_name AS product.Page 12 . In this lesson.country_id = countries.prod_id AND sales.time_id = times. The definition of sales_view is displayed as follows: DESCRIBE sales_view Name -------------------------PRODUCT COUNTRY CHANNEL QUARTER AMOUNT_SOLD QUANTITY_SOLD Null? -------NOT NULL NOT NULL NOT NULL Type -----------------VARCHAR2(50) VARCHAR2(40) NUMBER VARCHAR2(8) NUMBER NUMBER The calendar_quarter_desc column values in the SH schema are as follows: SELECT DISTINCT calendar_quarter_desc FROM times.time_id AND sales.prod_id = products. products WHERE sales. 6. channel_id. you use the newly created view. SUBSTR(calendar_quarter_desc. Performing Pivoting and Unpivoting Operations Chapter 4 . SUBSTR(calendar_quarter_desc.country_id GROUP BY prod_name.cust_id = customers. Oracle. SUM(amount_sold) AS amount_sold. country_name.

Note: The two-character quarter value is extracted from the calendar_quarter_desc column starting at position 6 as shown in the syntax in the example above. Performing Pivoting and Unpivoting Operations Chapter 4 . Copyright © 2007.Page 13 . Oracle. All rights reserved.1999-04 2002-02 2002-04 2000-02 2001-01 1998-02 1998-04 1999-01 2001-02 2001-03 2002-01 1999-03 1998-03 2000-01 20 rows selected.

. . . Oracle. SELECT DISTINCT channel_id. country. All rights reserved. Performing Pivoting and Unpivoting Operations Chapter 4 . CHANNEL_ID CHANNEL_DESC ---------. channel_desc FROM channels ORDER BY channel_id. quarter.Page 14 .Selecting the SALES_VIEW Data Selecting the SALES_VIEW Data SELECT product. representing the distinct products in the schema. . 9502 rows selected. Bounce Italy 2 01 34 Bounce Italy 2 02 43 . • The row count will drop to just 71 from 9502. PRODUCT COUNTRY CHANNEL QUARTER QUANTITY_SOLD -----------. .-------. The two boxes around QUARTER and “9502 rows selected” displayed in the second example above. Y Box Japan 2 01 35 Y Box Japan 2 02 39 Y Box Japan 2 03 36 Y Box Japan 2 04 46 Y Box Japan 3 01 65 .-----------.---------. channel.-------------------2 Partners 3 Direct Sales 4 Internet 5 Catalog 9 Tele Sales COLUMN QUARTER FORMAT A7 Copyright © 2007. quantity_sold FROM sales_view. The following displays the distinct channel_id and channel_desc column values from the CHANNEL table and the distinct QUARTER column values from the QUARTER table in the SH schema. each holding one quarter. . highlight the two key changes performed by the PIVOT operator: • The QUARTER column becomes multiple columns.------------Y Box Italy 4 01 21 Y Box Italy 4 02 17 Y Box Italy 4 03 20 .

QUARTER ------04 01 02 03 Copyright © 2007. All rights reserved.SELECT DISTINCT quarter FROM sales_view. Performing Pivoting and Unpivoting Operations Chapter 4 . Oracle.Page 15 .

'03'. quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter IN ('01'. and the measure alias. All rights reserved. '03' AS Q3. . The numbers shown in the pivoted output are the sum of quantity_sold for each product at each quarter. quarter. The result set shows the product column followed by a column for each value of quarter specified in the IN clause. the query outputs rows for each row of sales_view.---------. '04')) ORDER BY product DESC. '02' AS Q2. '02'. then the column name is a concatenation of the pivot column value or alias.---------.Page 16 . If you also specify an alias for each measure.---------.---------. 71 rows selected.Pivoting the QUARTER Column in the SH Schema: Example Pivoting the QUARTER Column in the SH Schema: Example SELECT * FROM (SELECT product.---------. quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter in ('01' AS Q1. PRODUCT Q1 Q2 Q3 Q4 ---------------. PRODUCT '01' '02' '03' '04' -----------------. This is needed because if you issue a SELECT * directly from the sales view. The example above uses a subquery (inline view) in the FROM clause. Performing Pivoting and Unpivoting Operations Chapter 4 . You can use aliases in the above example as shown below: SELECT * FROM (SELECT product.---------Y Box 1455 1766 1716 1992 Xtend Memory 3146 4121 4122 Unix/Windows 4259 3887 4601 4049 Standard Mouse 3376 1699 2654 2427 Copyright © 2007. quarter.---------Y Box 1455 1766 1716 1992 Xtend Memory 3146 4121 4122 Unix/Windows 1-user 4259 3887 4601 4049 Standard Mouse 3376 1699 2654 2427 Smash up Boxing 1608 2127 1999 2110 . Oracle. . an underscore (_). '04' AS Q4)) ORDER by PRODUCT DESC.---------.

ELSE ELSE ELSE ELSE NULL NULL NULL NULL END) END) END) END) Q1. 71 rows selected. the above example can be accomplished using the following syntax: SELECT product. Performing Pivoting and Unpivoting Operations Chapter 4 . All rights reserved.. Q4 The advantage of the new syntax over the syntax used before Oracle Database 11g is that it enables greater query optimization by Oracle. uses algorithms optimized to process it efficiently. quantity_sold FROM SALES_VIEW ) GROUP BY product. Oracle. . The query optimizer recognizes the PIVOT keyword and as a result. Copyright © 2007. quarter. Q2.Page 17 . Pivoting on the QUARTER Column: Example Before Oracle Database 11g. SUM(CASE when quarter = '01' THEN quantity_sold SUM(CASE when quarter = '02' THEN quantity_sold SUM(CASE when quarter = '03' THEN quantity_sold SUM(CASE when quarter = '04' THEN quantity_sold FROM ( SELECT product. . Q3.

SELECT * FROM pivot_table ORDER BY year.6 1999 1274078.---------1990 61655. inserts rows returned by the subquery. to get yearly totals by order mode. YEAR STORE INTERNET ---------.4 6 rows selected. All rights reserved. 'online' AS Internet)) ORDER BY year.orders table contains information about when an order was placed (order_date). The example.6 1997 310 1998 309929. Copyright © 2007. order_total FROM orders) PIVOT (SUM(order_total) FOR order_mode IN ('direct' AS Store. and the total amount of the order (order_total).7 1996 5546. The example shows how to use the PIVOT clause to pivot order_mode values into columns. pivot_table. and uses aliases for the direct and online pivot column values.Pivoting on the ORDER_MODE Column in the OE Schema: Example Pivoting on the ORDER_MODE Column in the OE Schema: Example CREATE TABLE pivot_table AS SELECT * FROM (SELECT EXTRACT(YEAR FROM order_date) year.---------. order_mode. aggregating order_total data in the process. The example creates a new table. Performing Pivoting and Unpivoting Operations Chapter 4 .8 1271019. Oracle. creates the new table. The oe.8 100056. as well as other information.3 393349. The example above uses the Order Enter (OE) schema.Page 18 . how it was placed (order_mode).5 2000 252108. using a subquery.

Copyright © 2007. Oracle.Pivoting on Multiple Columns Pivoting on Multiple Columns • To pivot on more than one column: – A pivoting column is required to be a column of the table reference on which the pivot is operating – The pivoting column cannot be an arbitrary expression • If you need to pivot on an expression. then you should use an alias for the expression in a view before the pivot operation. Performing Pivoting and Unpivoting Operations Chapter 4 . All rights reserved.Page 19 .

'04') AS Direct_Sales_Q4. '02') AS Direct_Sales_Q2. PRODUCT DIRECT_SALES_Q1 INTERNET_SALES_Q1 ------------------------. The example above pivots on both the CHANNEL and QUARTER columns. quarter) IN ((3. '03') AS Direct_Sales_Q3. (3. channel. '03') AS Internet_Sales_Q3. quarter. '02') AS Internet_Sales_Q2. (4.--------------. 71 rows selected. '01') AS Direct_Sales_Q1. quarter) IN ((3. . Copyright © 2007. . The example below specifies more values for the QUARTER column: SELECT * FROM (SELECT product. (3. '04') AS Internet_Sales_Q4)) ORDER BY product DESC. All rights reserved.Pivoting on Multiple Columns Pivoting on Multiple Columns SELECT * FROM (SELECT product. The example uses only the CHANNEL values 3 (Direct Sales) and 4 (Internet) and only the Q1 value for the QUARTER column. quarter. quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR (channel.----------------Y Box 771 253 Xtend Memory 1935 350 Unix/Windows 1-user pack 2544 397 Standard Mouse 2326 256 Smash up Boxing 1114 129 . (4. (4. '01') AS Internet_Sales_Q1. '01') AS Direct_Sales_Q1. (4. (4. (3.Page 20 . Oracle. channel. Performing Pivoting and Unpivoting Operations Chapter 4 . '01') AS Internet_Sales_Q1)) ORDER BY product DESC. quantity_sold FROM sales_view ) PIVOT (sum(quantity_sold) FOR (channel.

PRODUCT DIR_SALES_SUMS DIR_SALES_SUMQ INT_SALES_SUMS INT_SALES_SUMQ -----------.-------------.-------------Y Box 1081050.93 1878 Unix/Windows 1999882. All rights reserved. If you omit an alias.96 3552 382767..-------------. amount_sold.38 8562 40553. plus an underscore. you can omit the alias for only one aggregation.24 5106 27858. then the corresponding result column name is the pivot value (or the alias for the pivot value). Oracle.63 6140 28768.84 904 .62 1872 Standard Mouse 153199.Pivoting Using Multiple Aggregations Pivoting Using Multiple Aggregations SELECT * FROM (SELECT product.-------------. Copyright © 2007. SUM(quantity_sold) as sumq FOR channel IN (3 AS Dir_Sales. 71 rows selected. channel.04 1195 Smash up Boxing 174592. The query in the example above pivots SALES_VIEW on the CHANNEL column.17 9313 376071.. 4 AS Int_Sales)) ORDER BY product DESC. When you use multiple aggregations. quantity_sold FROM sales_view) PIVOT (SUM(amount_sold) AS sums.45 1339 Xtend Memory 217011.Page 21 . The amount_sold and quantity_sold measures get pivoted. Performing Pivoting and Unpivoting Operations Chapter 4 . Note that the query creates column headings by concatenating the pivot columns with the aliases of the aggregate functions.

amount_sold FROM sales2) PIVOT (SUM(amount_sold). All rights reserved. The definition of the sales2 table structure and the commands used to insert the data into sales2 are as follows: CREATE TABLE sales2 (prod_id NUMBER. amount_sold NUMBER). 'Q1'.Page 22 . 50). 'Q2'. 10). PROD_ID Q1 Q1_COUNT_TOTAL Q2 Q2_COUNT_TOTAL -----------------------------. INSERT INTO sales2 VALUES(100.Distinguishing PIVOT-Generated Nulls from Nulls in the Source Data Distinguishing PIVOT-Generated Nulls from Nulls in the Source Data SELECT * FROM sales2. Q1_COUNT_TOTAL. INSERT INTO sales2 VALUES(100. COUNT(*) AS count_total FOR qtr IN ('Q1'. For each unique value of prod_id. INSERT INTO sales2 VALUES(200. From the result set shown in the second example above. qtr. Oracle. Performing Pivoting and Unpivoting Operations Chapter 4 .---------------100 30 2 1 200 50 1 0 You can distinguish between NULL values that are generated from the use of PIVOT and those that exist in the source data. 'Q1'. Q2_COUNT_TOTAL. 20). 'Q1'. PROD_ID ------100 100 100 200 QTR --Q1 Q1 Q2 Q1 AMOUNT_SOLD ----------10 20 50 SELECT * FROM (SELECT prod_id. INSERT INTO sales2 VALUES(100. The unique value of Q2_COUNT_TOTAL returns the total number of rows whose QTR value is Q2. there are two sales rows for prod_id Copyright © 2007. null). qtr VARCHAR2(5). Q1_COUNT_TOTAL. The following example illustrates NULL that PIVOT generates. 'Q2') ) ORDER BY prod_id DESC. The query in the second code example returns rows prod_id and the resulting pivot columns: Q1. assumes an existing table named sales2. the query returns the total number of rows whose QTR value is Q1. The first code example above. Q2.

100 for quarter Q1. Copyright © 2007. The NULL for PROD_ID 200 in Q2 is due to no row being present in the original table for prod_id 200 in quarter Q2. there is one sales row for quarter Q1 and no sales row for quarter Q2.Page 23 . Performing Pivoting and Unpivoting Operations Chapter 4 . For prod_id 200. and one sales row for prod_id 100 and quarter Q2. All rights reserved. Oracle. you can identify that the NULL for PROD_ID 100 in Q2 is a result of a row in the original table whose measure is of NULL value. Using Q2_COUNT_TOTAL.

Copyright © 2007. The values of the pivot column are evaluated at execution time. Y AN Use the ANY keyword. Each output row will include the following: • The implicit Group By columns • A single column of XML Type containing an XML string for all value and measure pairs The XML string for each row will hold aggregated data corresponding to the row’s implicit GROUP BY value.Page 24 . Performing Pivoting and Unpivoting Operations Chapter 4 . All rights reserved. Oracle. or L XM If you use the XML keyword with the Pivot syntax Use a subquery The XML string includes all pivot values found by the subquery even if there are no aggregate values.Using the XML Keyword to Specify Pivot Values: Two Methods Using the XML Keyword to Specify Pivot Values: Two Methods The XML string for each row includes only the pivot values found in the input data for that row. Using the XML keyword in the Pivot syntax requires including either the ANY keyword or a subquery.

The XML output includes all channel values in the sales_view view created earlier in this lesson. All rights reserved. This output includes data for cases where the channel exists in the data set.5" Diskette <PivotSet> <item><column name = "CHANNEL">3</column><column name = "SUM(QUANTITY_SOLD)">14189</column></item> <item><column name = "CHANNEL">2</column><column name = "SUM(QUANTITY_SOLD)">6455</column></item> <item><column name = "CHANNEL">4</column><column name = "SUM(QUANTITY_SOLD)">2464</column></item></PivotSet> 71 rows selected. . all values found in the pivot column will be used for pivoting. PRODUCT -------------------------------------------------CHANNEL_XML -----------------------------------------------------------------------.Specifying Pivot Values: Using the ANY Keyword Specifying Pivot Values: Using the ANY Keyword SET LONG 1024. If you specify ANY. .44MB External 3. Oracle. Performing Pivoting and Unpivoting Operations Chapter 4 . Note that when using the ANY keyword. The ANY keyword acts as a wildcard. the ANY string for each output row will include only the pivot values found in the input data corresponding to that row. The example above uses the ANY wildcard keyword. The ANY keyword is only available in PIVOT operations as part of an XML operation. quantity_sold FROM sales_view ) PIVOT XML (SUM(quantity_sold) FOR channel IN (ANY) ) ORDER BY product DESC. Copyright © 2007. You can use wildcards or subqueries to specify the pivot IN list members when the values of the pivot column are not known. channel.Page 25 . 1. SELECT * FROM (SELECT product.

the XMLtype column will show value and measure pairs for all channels for each product even if the input data has no such product/channel combination. Note: The results in the above example do not show a complete row of output due to space limitations. Copyright © 2007. The example above illustrates the use of a subquery with the XML keyword. Oracle. quantity_sold FROM sales_view ) PIVOT XML(SUM(quantity_sold) FOR channel IN (SELECT distinct channel_id FROM channels)).Page 26 . the XML string in the slide shows the highlighted Channel 5 although it has no value for the SUM(QUANTITY_SOLD)column. Pivots that use a subquery will. subquery-style output may be more convenient to work with than the results derived from ANY. .The XML output includes all channel values and the sales data corresponding to each channel and for each product. Depending on how you process the query results. when you use a subquery. . In the example above. For example. . All rights reserved. . often have longer output than queries based on the ANY keyword.Specifying Pivot Values: Using Subqueries Specifying Pivot Values: Using Subqueries SELECT * FROM (SELECT product. therefore. Y Box <PivotSet> <item><column name = "CHANNEL">9</column><column name = "SUM(QUANTITY_SOLD)">1</column></item> <item><column name = "CHANNEL">2</column><column name = "SUM(QUANTITY_SOLD)">2037</column></item> <item><column name = "CHANNEL">5</column><column name = "SUM(QUANTITY_SOLD)"></column></item> <item><column name = "CHANNEL">3</column><column name = "SUM(QUANTITY_SOLD)">3552</column></item> . Performing Pivoting and Unpivoting Operations Chapter 4 . Subquery-based pivots give results different from those generated by the use of the ANY wildcard. channel. PRODUCT ---------CHANNEL_XML ----------------------------------------------------------------------.

500 The example above unpivots the QUARTER column.500 2.000 1. Unpivoting transforms fewer rows of input into generally more rows. All rights reserved. Oracle.Page 27 .500 2. If you are working with pivoted data.000 Q3 2. an UNPIVOT operation cannot reverse any aggregations that have been made by PIVOT or any other means.500 Q1 Q2 3. Performing Pivoting and Unpivoting Operations Chapter 4 . In the second example.000 2. thus turning the quarters columns into the values of the QUARTER column.Unpivoting the QUARTER Column: Conceptual Example Unpivoting the QUARTER Column: Conceptual Example PRODUCT Shorts Kids Jeans 2. instead. Note that the first example contains two rows before the unpivoting operation. the unpivoting operation on the QUARTER column displays five rows.500 2. Copyright © 2007.500 Q4 PRODUCT Shorts Shorts Kids Jeans Kids Jeans Kids Jeans QUARTER Q2 Q3 Q1 Q2 Q4 SUM_OF_QUANTITY 3. it rotates data found in multiple columns of a single row into multiple rows of a single column.000 1. An UNPIVOT operation does not reverse a PIVOT operation.

Copyright © 2007. Performing Pivoting and Unpivoting Operations Chapter 4 . it is more efficient to specify WHERE clause predicates on normalized data. transforming multiple columns into a single column. For instance. Oracle. When the data is normalized with UNPIVOT. UNPIVOT Data from sources such as spreadsheets and flat files is often in pivoted form.Page 28 . Similarly. All rights reserved. an UNPIVOT operation cannot reverse any aggregations that have been made by PIVOT or any other means.Using the UNPIVOT Operator Using the UNPIVOT Operator • An UNPIVOT does not reverse a PIVOT operation. queries can readily apply SQL aggregate and analytic functions. UNPIVOT can normalize such data. it rotates data from columns into rows. enabling powerful analysis. instead. By placing data in a normalized layout. it is much more accessible for relational database processing with SQL. sales data will often be stored in a separate column for each time period. • If you are working with pivoted data.

Page 29 . You specify: – The measure columns to be unpivoted – The names for the columns that will result from the unpivot operation – The columns that will be unpivoted back into values of the column specified in the pivot_for_clause • You can use an alias to map the column name to another value.Using the UNPIVOT Clause Using the UNPIVOT Clause • The UNPIVOT clause rotates columns from a previously pivoted table or a regular table into rows. All rights reserved. Copyright © 2007. Oracle. Performing Pivoting and Unpivoting Operations Chapter 4 .

then the unpivoted column is BINARY_DOUBLE. then the unpivoted column is BINARY_FLOAT. • If all the value columns are NUMBER. Oracle. then the unpivoted column is VARCHAR2. Therefore. Performing Pivoting and Unpivoting Operations Chapter 4 . Copyright © 2007. • If no value column is BINARY_DOUBLE but any value column is BINARY_FLOAT. the data types of all the value columns must be of the same data type such as numeric or character. • If any value column is VARCHAR2.The Data Types of the Value Columns in an UNPIVOT Operation The Data Types of the Value Columns in an UNPIVOT Operation Data Type for the Value Columns? If ALL the value columns are CHAR If ANY value column is VARCHAR2 If ALL the value columns are NUMBER If ANY value column is BINARY_DOUBLE Resulting Unpivoted Column Data Type CHAR VARCHAR2 NUMBER BINARY_DOUBLE BINARY_FLOAT If NO value column is BINARY_DOUBLE but ANY value column is BINARY_FLOAT The unpivot operation turns a set of value columns into one column.Page 30 . then the unpivoted column is CHAR. All rights reserved. • If all the value columns are CHAR. • If any value column is BINARY_DOUBLE. then the unpivoted column is NUMBER.

column].Specify one or more names for the columns that will -.the column specified in the pivot_for_clause. ) } -. such as quarter or product. ) } unpivot_for_clause unpivot_in_clause ) -..) The UNPIVOT clause rotates columns into rows. ) } [ AS { constant | ( constant [. For column.. specify the input data columns whose names will become values in the output columns of the pivot_for_clause. The [INCLUDE] | [EXCLUDE] [NULLS] clause gives you the option of including or excluding nullvalued rows. unpivot_for_clause = FOR { column | ( column [..) } ] ].The UNPIVOT Clause Syntax The UNPIVOT Clause Syntax table_reference UNPIVOT [{INCLUDE|EXCLUDE} NULLS] -. These input data columns have names specifying a category value. ) } [ AS { constant | ( constant [. specify a name for each output column that will hold descriptor values. and Q4. Q2.result from the unpivot operation. All rights reserved.specify the measure column(s) to be unpivoted. Performing Pivoting and Unpivoting Operations Chapter 4 .. constant]. Q3. column].. ) } ] [.Specify the columns that will be unpivoted into values of -... { column | ( column [. specify a name for each output column that will hold measure values. [EXCLUDE] [NULLS] eliminates nullvalued rows from the return set. Oracle. constant]. such as Q1... The optional alias enables you to map the column name to any desired value.. In the pivot_for_clause. then the unpivot operation excludes nulls. ( { column | ( column [. column]..Page 31 . unpivot_in_clause = ( { column | ( column [. column]. In the unpivot_in_clause. such as sales_quantity.. [INCLUDE] [NULLS] causes the unpivot operation to include nullvalued rows. Copyright © 2007... If you omit this clause.

quarter. Oracle. '04' AS Q4)). Performing Pivoting and Unpivoting Operations Chapter 4 .---------.Page 32 . Table created. '03' AS Q3. .---------Y Box 1455 1766 1716 1992 Xtend Memory 3146 4121 4122 3802 . quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter IN ('01' AS Q1. PRODUCT Q1 Q2 Q3 Q4 -----------------.---------. All rights reserved. The example above creates a new table named pivotedtable.---------. 71 rows selected.Creating a New Pivot Table: Example Creating a New Pivot Table: Example CREATE TABLE pivotedtable AS SELECT * FROM (SELECT product. '02' AS Q2. Copyright © 2007. SELECT * FROM pivotedtable ORDER BY product DESC. .

'Q2' as FROM pivotedTable WHERE Q2 is not NULL union all SELECT product. 283 rows selected. Copyright © 2007. quarter. Q3 as quantity_sold quarter. Q4)) ORDER BY product DESC.------------Q1 1455 Q2 1766 Q3 1716 Q4 1992 Q1 3146 Q2 4121 Q3 4122 Q4 3802 Q1 4259 The QUARTER column in the above example was formatted using the following command: COLUMN quarter FORMAT A7 Before Oracle Database 11g. Oracle. Q3. All rights reserved. the optimizer calls highly efficient algorithms. PRODUCT -------------------------Y Box Y Box Y Box Y Box Xtend Memory Xtend Memory Xtend Memory Xtend Memory Unix/Windows 1-user pack . quarter. As a result. Performing Pivoting and Unpivoting Operations Chapter 4 . QUARTER QUANTITY_SOLD ------.Page 33 . 'Q1' as FROM pivotedTable WHERE Q1 is not NULL union all SELECT product. Q2. . . Q4 as quantity_sold However the UNPIVOT syntax enables more efficient query processing.Unpivoting the QUARTER Column in the SH Schema: Example Unpivoting the QUARTER Column in the SH Schema: Example SELECT * FROM pivotedtable UNPIVOT (quantity_sold For Quarter IN (Q1. 'Q3' as FROM pivotedTable WHERE Q3 is not NULL union all SELECT product. Q2 as quantity_sold quarter. 'Q4' as FROM pivotedTable WHERE Q4 is not NULL. you can simulate the UNPIVOT syntax in the above example using existing SQL as shown below: SELECT product. Q1 as quantity_sold quarter. The UNPIVOT keyword alerts the query optimizer to the desired behavior.

Oracle.6 310 309929. Performing Pivoting and Unpivoting Operations Chapter 4 . The UNPIVOT clause enables you to restore a pivoted table. Copyright © 2007.Unpivoting the ORDER_MODE Column in the OE Schema: Example Unpivoting the ORDER_MODE Column in the OE Schema: Example SELECT * FROM pivot_table UNPIVOT (yearly_total FOR order_mode IN (store AS 'direct'.Page 34 . so that selected columns are pivoted into values in a single column.8 1271019. YEAR ----1990 1996 1997 1998 1998 1999 1999 2000 2000 ORDER_MODE ----------direct direct direct direct online direct online direct online YEARLY_TOTAL -----------61655. Note the use of column aliases in the above example.4 9 rows selected.6 1274078. order_mode. internet AS 'online')) ORDER BY year.3 393349. or a table with similar structure. All rights reserved.8 100056.7 5546.5 252108.

-------PRODUCT NOT NULL DIRECT_SALES_Q1 INTERNET_SALES_Q1 Type -----------------------VARCHAR2(50) NUMBER NUMBER The query in the example above returns 71 rows. '01') AS Direct_Sales_Q1. Oracle. Copyright © 2007. . . SELECT * FROM multi_col_pivot. Performing Pivoting and Unpivoting Operations Chapter 4 .----------------Y Box 771 253 Xtend Memory 1935 350 . channel. (4. You can use other values for both columns.Unpivoting Multiple Columns in the SH Schema: Example Unpivoting Multiple Columns in the SH Schema: Example CREATE TABLE multi_col_pivot AS SELECT * FROM (SELECT product. quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR (channel. The example uses only the CHANNEL values 3 (Direct Sales) and 4 (Internet) and only the Q1 value for the QUARTER column. quarter. The example above creates a pivot table using the CHANNEL and QUARTER columns in the SH schema. 71 rows selected. The structure of the newly created table is shown below: DESCRIBE multi_col_pivot Name Null? --------------------.--------------. '01') AS Internet_Sales_Q1)) ORDER BY product DESC. Table created. PRODUCT DIRECT_SALES_Q1 INTERNET_SALES_Q1 ---------------------------. All rights reserved. This is similar to the “Pivoting Using Multiple Columns” example mentioned earlier. quarter) IN ((3.Page 35 .

Oracle. Note that the example uses explicit values for the unpivoted CHANNEL and QUARTER columns. SELECT * FROM multi_col_pivot UNPIVOT (quantity_sold For (channel. AS ('Internet'. quarter) IN AS ('Direct'.------------DIRECT_SALES_Q1 771 INTERNET_SALES_Q1 253 DIRECT_SALES_Q1 1935 Copyright © 2007.----------------Y Box DIRECT_SALES_Q1 Y Box INTERNET_SALES_Q1 Xtend Memory DIRECT_SALES_Q1 .. Internet_Sales_Q1 ) ). . PRODUCT -----------------------------Y Box Y Box Xtend Memory Xtend Memory . In this case.. each unpivoted column uses the column name as its value. Performing Pivoting and Unpivoting Operations Chapter 4 .Provide explicit values for the unpivot columns SELECT * FROM multi_col_pivot UNPIVOT (quantity_sold ( Direct_Sales_Q1 Internet_Sales_Q1 ORDER BY product DESC.------------Q1 253 Q1 771 Q1 350 Q1 1935 The example above unpivots the CHANNEL and QUARTER columns using the multi_col_pivot table created on the previous page. All rights reserved. CHANNEL -------Internet Direct Internet Direct QUARTER QUANTITY_SOLD ------. quarter) IN (Direct_Sales_Q1. 'Q1') ) ) quarter. For (channel. 142 rows selected. 'Q1').-. PRODUCT CHANNEL -----------. The example below demonstrates unpivoting on the CHANNEL and QUARTER columns without using aliases as explicit values for the unpivoted columns.Page 36 . . QUARTER QUANTITY_SOLD ----------------. 142 rows selected. The query in the example above returns 142 rows.

37 37781.97 26417. . This is similar to the “Pivoting Using Multiple Aggregates” example mentioned earlier.Unpivoting Multiple Aggregations in the SH Schema: Example Unpivoting Multiple Aggregations in the SH Schema: Example CREATE TABLE multi_agg_pivot AS SELECT * FROM (SELECT product. You can use other values for the CHANNEL column. Note that the query creates column headings by concatenating the pivot columns with the aliases of the aggregate functions.--------PRODUCT NOT NULL QUARTER DIRECT_SUMQ DIRECT_SUMA INTERNET_SUMQ INTERNET_SUMA Type ------------------------VARCHAR2(50) VARCHAR2(8) NUMBER NUMBER NUMBER NUMBER Copyright © 2007.------------. . The example uses only the CHANNEL values 3 (Direct Sales) and 4 (Internet).63 347 453 528 632 6948.07 The example shown above creates the multi_agg_pivot table using the CHANNEL column and the amount_sold and quantity_sold measures in the SH schema. .76 9173. quarter. The structure of the newly created table is shown below: DESCRIBE multi_agg_pivot Name Null? ------------------. sum(amount_sold) suma FOR channel IN (3 AS Direct. plus an underscore.------. you can omit the alias for only one aggregation. then the corresponding result column name is the pivot value (or the alias for the pivot value). 283 row selected. Table created. amount_sold FROM sales_view) PIVOT (sum(quantity_sold) sumq.59 10029. quantity_sold.Page 37 . PRODUCT QUARTER ---------. 4 AS Internet) ) ORDER BY product DESC.------------1000 1212 1746 1741 21738. channel. SELECT * FROM multi_agg_pivot. Performing Pivoting and Unpivoting Operations Chapter 4 .----------. Bounce 01 Bounce 02 Bounce 03 Bounce 04 . . If you omit an alias.27 38838. All rights reserved. DIRECT_SUMQ DIRECT_SUMA INTERNET_SUMQ INTERNET_SUMA ----------.99 12592. Oracle. When you use multiple aggregations.

------. 566 rows selected.----------------. total_quantity_sold) For channel IN ((Direct_sumq.Page 38 . The channel mapping is consistent with the values used in the pivot operation that created the multi_agg_pivot table.07 . Internet_suma) AS 4 )) ORDER BY product DESC.63 Bounce 04 4 632 12592. All rights reserved.76 Bounce 02 3 1212 26417. The Unpivot example shown above uses the newly created multi_agg_pivot table. Channels are mapped to the value “3” for Direct_sumq and Direct_suma and to the value “4” for Internet_sumq and Internet_suma. Copyright © 2007.SELECT * FROM multi_agg_pivot UNPIVOT ((total_amount_sold. quarter. The measures amount_sold and quantity_sold are unpivoted. (Internet_sumq. PRODUCT QUARTER CHANNEL TOTAL_AMOUNT_SOLD TOTAL_QUANTITY_SOLD ------. Oracle.37 Bounce 02 4 453 9173.99 Bounce 04 3 1741 38838.27 Bounce 03 4 528 10029.------------------Bounce 01 3 1000 21738. .59 Bounce 03 3 1746 37781.------.97 Bounce 01 4 347 6948. Direct_suma) AS 3. any values could have been used for the channel mappings. . Performing Pivoting and Unpivoting Operations Chapter 4 . channel. However.

All rights reserved. Oracle.Page 39 . Performing Pivoting and Unpivoting Operations Chapter 4 .Summary Summary In this lesson. you should have learned how to: • Identify the benefits of pivoting and unpivoting operations • Write crosstab queries to pivot (rotate) column values into new columns and to unpivot (rotate) columns into column values • Pivot and unpivot with multiple columns and multiple aggregates • Use wildcards and aliases with pivoting operations Copyright © 2007.

Copyright © 2007. All rights reserved. Oracle. Performing Pivoting and Unpivoting Operations Chapter 4 .Page 40 .

Sign up to vote on this title
UsefulNot useful