Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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:
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
John Haydu, Lei Sheng, Lilian Hobbs, Paul Lane, Diana Lorentz, Christine Jeal
i
Chapter 4Performing Pivoting and Unpivoting Operations ..........................................................................4-2
Objectives......................................................................................................................................................4-3
The Order Entry (OE) Schema Used in This Lesson ....................................................................................4-4
The Sales History (SH) Schema Used in This Lesson ..................................................................................4-5
The Sales History (SH) Schema Used in this eStudy ....................................................................................4-6
The Benefits of Using Pivoting Operations...................................................................................................4-7
The PIVOT and UNPIVOT Clauses of the SELECT Statement..................................................................4-8
Pivoting on the QUARTER Column: Conceptual Example.........................................................................4-9
The PIVOT Clause Syntax ............................................................................................................................4-10
Creating a New View: Example ....................................................................................................................4-12
Selecting the SALES_VIEW Data ................................................................................................................4-14
Pivoting the QUARTER Column in the SH Schema: Example ...................................................................4-16
Pivoting on the ORDER_MODE Column in the OE Schema: Example......................................................4-18
Pivoting on Multiple Columns ......................................................................................................................4-19
Pivoting Using Multiple Aggregations..........................................................................................................4-21
Distinguishing PIVOT-Generated Nulls from Nulls in the Source Data......................................................4-22
Using the XML Keyword to Specify Pivot Values: Two Methods ..............................................................4-24
Specifying Pivot Values: Using the ANY Keyword .....................................................................................4-25
Specifying Pivot Values: Using Subqueries..................................................................................................4-26
Unpivoting the QUARTER Column: Conceptual Example .........................................................................4-27
Using the UNPIVOT Operator......................................................................................................................4-28
Using the UNPIVOT Clause .........................................................................................................................4-29
The Data Types of the Value Columns in an UNPIVOT Operation ............................................................4-30
The UNPIVOT Clause Syntax ......................................................................................................................4-31
Creating a New Pivot Table: Example ..........................................................................................................4-32
Unpivoting the QUARTER Column in the SH Schema: Example ..............................................................4-33
Unpivoting the ORDER_MODE Column in the OE Schema: Example ......................................................4-34
Unpivoting Multiple Columns in the SH Schema: Example ........................................................................4-35
Unpivoting Multiple Aggregations in the SH Schema: Example.................................................................4-37
Summary .......................................................................................................................................................4-39
ii
Partitioning and Storage-
Related Enhancements
Objectives
Oracle Partitioning
• Interval partitioning
• System partitioning
• Composite partitioning enhancements
• Virtual column-based partitioning
• Reference partitioning
Partitioning allows the DBA to employ a “divide and conquer” methodology for managing
database tables, especially as those tables grow. Partitioned tables allow a database to scale for
very large datasets while maintaining consistent performance, without unduly impacting
administrative or hardware resources.
Partitioning enables faster data access within an Oracle database. Whether a database has 10 GB
or 10 TB of data, partitioning can speed up data access by orders of magnitude.
With the introduction of Oracle Database 11g, the DBA will find a useful assortment of
partitioning enhancements. 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. On import, you can choose to load partitions as is, merge them into a single
table, or promote each into a separate table. Please refer to the Oracle® Database Utilities 11g
Release 1 (11.1) guide for more information on Data Pump.
Interval Partitioning
Before the introduction of interval partitioning, the DBA was required to explicitly define the
range of values for each partition. The problem is explicitly defining the bounds for each partition
does not scale as the number of partitions grow.
Interval partitioning is an extension of range partitioning, which instructs the database to
automatically create partitions of a specified interval when data inserted into the table exceeds all
of the range partitions. You must specify at least one range partition. The range partitioning key
value determines the high value of the range partitions, which is called the transition point, and
the database creates interval partitions for data beyond that transition point.
Interval partitioning fully automates the creation of range partitions. Managing the creation of
new partitions can be a cumbersome and highly repetitive task. This is especially true for
predictable additions of partitions covering small ranges, such as adding new daily partitions.
Interval partitioning automates this operation by creating partitions on demand.
When using interval partitioning, consider the following restrictions:
• You can specify only one partitioning key column, and it must be of NUMBER or DATE type.
• Interval partitioning is not supported for index-organized tables.
• You cannot create a domain index on an interval-partitioned table.
Automatically created
when you insert data
P1 P2 P3 Pi1 … Pin
Consider the example above, which illustrates the creation of an interval-partitioned table. The
original CREATE TABLE statement specifies four partitions with varying widths.
This portion of the table is range-partitioned. It also specifies that above the transition point of “1-
1-2007,” partitions be created with a width of one month. These partitions are interval-
partitioned. Partition Pi1 is automatically created using this information when a row with a value
corresponding to January 2007 is inserted into the table. The high bound of partition P3
represents a transition point. P3 and all partitions below it (P1 and P2 in this example) are in the
range section, whereas all partitions above it fall into the interval section. The only argument to
the INTERVAL clause is a constant of the interval type. Currently, you can specify only one
partitioning key column, 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.
PREVIOUS
< 01/01/07 INSERT INTO orders_interval (…);
Transition point
Not yet
PREVIOUS materialized
SYS_Px SYS_Py SYS_Pz SYS_Pt
< 01/01/07 < 01/08/07 < 01/11/07 < 01/12/07 < 01/03/08
Transition point
The graphic in the slide shows you a typical Information Lifecycle Management (ILM) scenario
where after one year of automated partition creation, you merge the created partitions (SYS_Py
and SYS_Pz in the example) to move the transition point. You can then move the resulting
partitions to a different storage for ILM purposes.
The example assumes that you created a table called ORDERS_INTERVAL that has initially one
range partition called PREVIOUS, which holds orders from before 2007. The interval is defined
to be one month. Then during the year 2007 and 2008, some orders are inserted, and it is assumed
that four partitions are created. They are shown in the graphic. They are automatically named
according to certain system rules.
You then decide to merge the last two partitions of the year 2007 using the ALTER TABLE
statement shown in the slide. You must merge two adjacent partitions. The new extended
partition syntax is used to designate a partition without needing to know the partition name.
The syntax uses an expression that must represent a possible value for the partition in question.
This syntax works for all cases when you have to reference a partition, whether it is range, list,
interval, or hash. It supports all operations such as drop, merge, split, and so on.
As a result of your MERGE operation, you can see that the transition point moved. The bottom
part of the graphic shows you the new range section that now contains three partitions.
Note: You can change the interval of an interval-partitioned table; the existing intervals remain
unaffected.
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. This is mainly
useful when you develop your own partitioned domain indexes. The database simply provides the
ability to break down a table into meaningless partitions. All other aspects of partitioning are
controlled by the application. System partitioning provides the well-known benefits of
partitioning (scalability, availability, and manageability), but the partitioning and actual data
placement are controlled by the application.
The most fundamental difference between system partitioning and other methods is that system
partitioning does not have any partitioning keys. Consequently, the distribution or mapping of the
rows to a particular partition is not implicit. Instead, the user specifies the partition to which a
row maps by using partition-extended syntax when inserting a row.
Because system-partitioned tables do not have a partitioning key, the usual performance benefits
of partitioned tables are not available for system-partitioned tables. Specifically, there is no
support for traditional partition pruning, partitionwise joins, and so on. Partition pruning is
achieved by accessing the same partitions in the system-partitioned tables as those that were
accessed in the base table.
System-partitioned tables provide the manageability advantages of equipartitioning. For example,
a nested table can be created as a system-partitioned table that has the same number of partitions
as the base table. A domain index can be backed up by a system-partitioned table that has the
same number of partitions as the base table.
The syntax in the slide example creates a table with four partitions. Each partition can have
different physical attributes. INSERT and MERGE statements must use partition-extended syntax
to identify a particular partition that a row should go into. For example, the value (4,5) can be
inserted into any one of the four partitions given in the example.
Deletes and updates do not require the partition-extended syntax. However, because there is no
partition pruning, if the partition-extended syntax is omitted, the entire table is scanned to execute
the operation. Again, this example highlights the fact that there is no implicit mapping from rows
to any partition.
Columns of a table whose values are derived by computation of a function or an expression are
known as virtual columns. These columns can be specified during a CREATE or ALTER table
operation. 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. These columns can be
used in queries like any other table columns, thereby providing a simple, 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, rather they are
evaluated on demand. The functions or expressions describing the virtual columns should be
deterministic and pure, meaning the same set of input values should return the same output
values.
Virtual columns can be used like any other table columns. They can be indexed, and used in
queries, DML, and DDL statements. Tables and indexes can be partitioned on a virtual column
and statistics can be gathered upon them.
You can use virtual column partitioning to partition key columns defined on virtual columns of a
table. Frequently, business requirements to logically partition objects do not match existing
columns in a one-to-one manner. With the introduction of Oracle Database 11g, partitioning has
been enhanced to allow a partitioning strategy defined on virtual columns, thus enabling a more
comprehensive match of the business requirements.
Consider the example in the slide. The EMPLOYEES table is created using the standard CREATE
TABLE syntax. The total_compensation column is a virtual column calculated by
multiplying the value of salary by the commission_pct plus one. The next statement
declares total_compensation (a virtual column) to be the partitioning key of the
EMPLOYEES table.
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, the optimizer recognizes when a partitionwise join
(full or partial) is applicable, decides whether to use it or not, and annotates the join properly
when it decides to use it. This applies to both serial and parallel cases.
To recognize full partitionwise joins, the optimizer relies on the definition of equipartitioning of
two objects; this definition includes the equivalence of the virtual expression on which the tables
were partitioned.
Reference Partitioning
Reference partitioning provides the ability to partition a table based on the partitioning scheme of
the table referenced in its referential constraint. The partitioning key is resolved through an
existing parent/child relationship, which is enforced by active primary key or foreign key
constraints. 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. The logical dependency also automatically cascades partition maintenance
operations, making application development easier and less error prone.
… Range (ORDER_DATE) …
Primary key (ORDER_ID)
Table ORDERS
Table
ORDER_ITEMS
… Range (ORDER_DATE) …
Foreign key (ORDER_ID)
As illustrated in the slide, you can see the benefit of using reference partitioning. The left part of
the graphic shows you the situation where you have two tables, ORDERS and ORDER_ITEMS,
that are equipartitioned on the ORDER_DATE column. In that case, both tables need to define the
ORDER_DATE column. However, defining ORDER_DATE in the ORDER_ITEMS table is
redundant because there is a primary key/foreign key relationship between the two tables.
The right part of the graphic shows you the situation where you use reference partitioning. This
time, you no longer need to define the ORDER_DATE column in the ORDER_ITEMS table. The
partition key of the ORDER_ITEMS table is automatically inherited from the primary key/foreign
key relationship that exists.
When used for pruning and partitionwise joins, reference partitioning has the benefit that query
predicates can be different and partitionwise joins still work—for example, partitioning on
ORDER_DATE and search on ORDER_ID. With previous releases, both partitioning and
predicates had to be identical for a partitionwise join to work.
Note: This partitioning method can be useful for nested table partitioning.
– List-Hash
– List-Range SP2 SP2 SP2 … SP2 SP2
Before the release of Oracle Database 11g, the only composite partitioning methods supported
were range-list and range-hash. With this new release, list partitioning can be a top-level
partitioning method for composite partitioned tables giving us list-list, list-hash, list-range, and
range-range composite methods. With the introduction of interval partitioning, interval-range,
interval-list, and interval-hash are now supported composite partitioning methods.
Range-Range Partitioning
Composite range-range partitioning enables logical range partitioning along two dimensions; for
example, range partition by order_date and range subpartition by shipping_date.
List-Range Partitioning
Composite list-range partitioning enables logical range subpartitioning within a given list
partitioning strategy; for example, list partition by country_id and range subpartition by
order_date.
List-Hash Partitioning
Composite list-hash partitioning enables hash subpartitioning of a list-partitioned object; for
example, to enable partitionwise joins.
List-List Partitioning
Composite list-list partitioning enables logical list partitioning along two dimensions; for
example, list partition by country_id and list subpartition by sales_channel.
Composite range-range partitioning enables logical range partitioning along two dimensions. In
the example in the slide, the SALES table is created and range-partitioned on time_id. Using a
subpartition template, the SALES table is subpartitioned by range using cust_id as the
subpartition key.
Because of the template, all partitions have the same number of subpartitions with the same
bounds as defined by the template. If no template is specified, a single default partition bound by
MAXVALUE (range) or DEFAULT value (list) is created.
Although the example illustrates the range-range methodology, the other new composite
partitioning methods use similar syntax and statement structure. All of the composite partitioning
methods fully support the existing partition pruning methods for queries involving predicates on
the subpartitioning key.
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. Using this feature, you
could compress data at the time of performing bulk load using operations such as direct loads, or
Create Table As Select (CTAS). However, until now, compression was not available for regular
data manipulation operations such as INSERT, UPDATE, and DELETE. Oracle Database 11g
extends the compression technology to support these operations as well. Consequently,
compression in Oracle Database 11g can be used for all kinds of workload, be it online
transaction processing (OLTP) or data warehousing.
It is important to mention that table compression enhancements introduced in Oracle Database
11g are not just incremental changes. 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. As a
result, compression technology in Oracle Database 11g is very efficient and could reduce the
space consumption by 50–75%. And while you do that, not only your write performance does not
degrade, but also your read performance or queries improve. This is because unlike desktop-
based compression techniques where you have to wait for data to be uncompressed, Oracle
technology reads the compressed data (less fetches needed) directly and does not require any
uncompress operation.
Note: Compression technology is completely application transparent. This means that you can
use this technology with any homegrown or packaged application such as SAP, Siebel, EBS, and
so on.
Compressed
data
PCTFREE reached,
compression triggered
Uncompressed
data
Free
space
Inserts are
uncompressed.
The slide shows you a data block evolution when that block is part of a compressed table. You
should read it from left to right. At the start, the block is empty and available for inserts.
When you start inserting into this block, data is stored in an uncompressed format (like for
uncompressed tables). However, as soon as you reach the PCTFREE of that block, the data is
automatically compressed, potentially reducing the space it originally occupied. This allows for
new uncompressed inserts to take place in the same block, until PCTFREE is reached again. At
that point compression is triggered again to reduce space occupation in the block.
Note: Compression eliminates holes created due to deletions, and maximizes contiguous free
space in blocks.
To use the new compression algorithm, you must flag your table with the COMPRESS FOR ALL
OPERATIONS clause. You can do so at table creation, or after creation. This is illustrated in the
examples given above.
If you use the COMPRESS clause without specifying any FOR option, or if you use the
COMPRESS FOR DIRECT_LOAD OPERATIONS clause, you fall back to the old compression
mechanism that was available in earlier releases.
You can also enable compression at the partition or tablespace level. For example, you can use
the DEFAULT storage clause of the CREATE TABLESPACE command to optionally specify a
COMPRESS FOR clause.
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.
Summary
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 0 - Page 1
Chapter 2Using the New and Updated Materialized Views (MV)
Catalog Views
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 2
Objectives
Objectives
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 3
The New and Updated MV Catalog Views
Note: For additional information about materialized views and partition change tracking, 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.1) documentation.
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 4
PCT Catalog Views Showing Staleness Corresponding to Base
Partitions
USER/ALL/DBA_MVIEWS USER/ALL/DBA_MVIEW_DETAIL
views (UPDATED) _RELATIONS views (UPDATED)
USER/ALL/DBA_MVIEW_DETAIL_ USER/ALL/DBA_MVIEW_DETAIL_
PARTITION views (NEW) 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. Therefore, users are unable to see this
valuable information, which could help them make better decisions as to how their materialized
views should be maintained.
Oracle Database 11g exposes the MV freshness information corresponding to PCT base table
partitions. This provides users with the information regarding which ranges of the MV data are
fresh and which are not. The fresh part of MV is reliable and available for use.
The USER/ALL/DBA_MVIEWS catalog view is extended where new columns are added to
describe number of PCT tables, number of fresh and stale PCT regions.
The USER/ALL/DBA_DETAIL_RELATIONS catalog view is extended where new columns are
added to indicate if the detail table is PCT-enabled, and to show numbers of fresh and stale PCT
partitions.
A new catalog view for PCT partition (USER/ALL/DBA_MVIEW_DETAIL_PARTITION) is
created to describe the freshness of each PCT partition.
Create a new catalog view for PCT subpartition
(USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION) to describe the freshness of each PCT
subpartition.
The USER/ALL/DBA_MVIEWS extension describes all materialized views in the database.
The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS extension represents the named detail
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 5
relations that are either in the FROM list of a materialized view or that are indirectly referenced
through views in the FROM list.
The new USER/ALL/DBA_MVIEW_DETAIL_PARTITION displays the freshness information
of the materialized views, with respect to a PCT detail partition.
The new USER/ALL/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 6
The New Columns in the USER/ALL/DBA_MVIEWS Catalog View
. . . . . .
The syntax shown above is the extended specification for the DBA/ALL/USER_MVIEWS catalog
view. This catalog view is extended to show how many detail partitions support PCT. In addition,
two extended columns show how many fresh and stale PCT regions are present in that MV.
The ALL_MVIEWS describes all materialized views accessible to the current user. The
DBA_MVIEWS describes all materialized views in the database. The USER_MVIEWS describes
all materialized views owned by the current user.
Note: For the complete columns listing for the DBA/ALL/USER_MVIEWS catalog views, see the
Oracle Database Reference 11g Release 1 (11.1) guide.
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 7
The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS Catalog View
Extension
The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS
Catalog View Extension
Catalog View Columns Column Description
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 8
The New Catalog View:
USER/ALL/DBA_MVIEW_DETAIL_PARTITION
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 9
The New Catalog View:
USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 10
Refresh Performance Improvements in Oracle Database 11g
The refresh performance improvements reduce the time required to refresh materialized views.
Query Rewrite Possible Using the Materialized View During Its Atomic Refresh
Currently, when the materialized view is being refreshed, it is implicitly disabled for query
rewrite even if its data is acceptable for the user. 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 11g, we plan to address the issue for the atomic refresh so that when the
materialized view is refreshed in the atomic mode, it is eligible for query rewrite if the rewrite
integrity mode is set to “STALE_TOLERATED”.
Partition-Based Refresh for UNION ALL MVs
In Oracle Database 9i Release 2, the fast refresh was extended to support the MV that has UNION
ALL operators. However, the fast refresh of the UNION ALL MV did not apply partition-based
(PCT) refresh. Using the PCT refresh can be beneficial during the refresh of UNION ALL MV. In
Oracle Database 11g, PCT refresh is now allowed for UNION ALL MV fast refresh.
Automatically Create Proper Index on MV (UNION ALL MV)
Currently, the materialized view with UNION ALL operators is fast refreshable but unlike most
of the fast refreshable MVs, it is not created with an index. To speed up the refresh execution, the
user needs to manually create such an index. In Oracle Database 11g, an index is created
automatically during MV creation.
Note: For information about refreshing materialized views, see the Oracle Database 10g:
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 11
Implement and Administer a Data Warehouse Instructor-Led Training or the Oracle Database
Data Warehousing Guide 11g Release 1 (11.1) documentation.
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 12
Summary
Summary
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 13
Copyright © 2007, Oracle. All rights reserved.
Using the New and Updated Materialized Views (MV) Catalog Views
Chapter 2 - Page 14
General Query Rewrite
Enhancements
Objectives
Summaries are aggregate views that are created to improve query execution times. When you use
a summary, the results of joins and aggregate operations are calculated before query execution
and stored in a database table. In an Oracle database, summaries are implemented with a
materialized view.
There are many well-known techniques you can use to increase query performance. For example,
you can create additional indexes, or you can partition your data.
Many data warehouses are also using a technique called summaries. 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, which is comparable to a CREATE TABLE AS SELECT
(CTAS) statement. Instead of precomputing the same query result many times, the user can
directly access the summary table. Although this approach has the benefit of enhancing query
response time, it also has many drawbacks. The user needs to be aware of the summary table’s
existence in order to rewrite the query to use that table instead. Also, the data contained in a
summary table is frozen, and must be manually refreshed whenever modifications occur on the
real tables.
With Oracle Database summary management, the end user no longer has to be aware of the
summaries that have been defined. The DBA creates materialized views that are automatically
used by the system when rewriting SQL queries. Using materialized views offers another
advantage over manually creating summaries tables, in that the data can be refreshed
automatically.
Note: The term summary comes from the fact that most of the time users in data warehouse
Summary Management
In a typical use of summary management, the database administrator creates the materialized
view (summary table). When the end user queries tables and views, the Oracle server query
rewrite mechanism automatically rewrites the SQL query to use the summary table. The use of
the materialized view is transparent to the end user or application querying the data.
The implementation of summary management in Oracle Database includes the use of the
components listed above.
After your data has been transformed, staged, and loaded into the detail tables, you invoke the
summary management process as described above.
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. The optimizer rewrites a query by
rewriting one or more query blocks, one at a time.
If the rewrite logic has a choice between multiple materialized views to rewrite a query block, it
selects the one that can result in reading the least amount of data.
After a materialized view has been picked for a rewrite, 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). This process continues until
no further rewrites are possible. Query rewrite is attempted recursively to take advantage of
nested materialized views.
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, DELETE, and UPDATE.
It also operates on subqueries in set operators: UNION, UNION ALL, INTERSECT, and MINUS.
Yes
No
Rewrite query
No Yes
Oracle Database 10g supported general query rewrite when the user query contained an inline
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.
In that case, Oracle Database 10g treated the matching inline view as it would a named view, and
general rewrite processing was possible.
The query rewrite in Oracle Database 11g supports queries containing inline views. More queries
are now eligible for query rewrite thus improving system throughput and performance.
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, or
• When the request query contains inline views that are equivalent to the inline views in the
materialized view
The first example shown above, displays a materialized view that contains an inline view.
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; therefore, the query inline view will be internally
replaced with the materialized view's inline view so that the query can be rewritten.
The first example shown above, displays a materialized view that contains an inline view.
The second example shown above, displays a query that has an equivalent inline view to the
inline view found in the materialized view example above. Note that their texts do not match. It
gets the same object number and rewrite takes place.
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. Note that the join predicate in the query inline
view is switched. Even though this query does not textually match with that of the materialized
view's inline view, query rewrite will identify the query's inline view as equivalent to the
materialized view's inline view. As before, the query inline view will be internally replaced with
the materialized view's inline view so that the query can be rewritten.
Both of the two earlier queries are rewritten with SUM_SALES_MV as shown in the code example
above.
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. 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.
Use the
DBMS_MVIEW.EXPLAIN_
REWRITE procedure.
Because query rewrite occurs transparently, it is not always evident that it has taken place.
The rewritten statement is not stored in the V$SQL view, nor can it be dumped in a trace file. Of
course, if the query runs faster, rewrite should have occurred; but there is no proof to confirm
this.
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.
• Use the DBMS_MVIEW.EXPLAIN_REWRITE procedure to see whether a query will be
rewritten or not.
Note: For additional information about verifying whether or not the query was rewritten using the
EXPLAIN PLAN statement and the DBMS_MVIEW.EXPLAIN_REWRITE procedure, 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.1) documentation.
Oracle Database 11g extends the current query rewrite capability. Query rewrite can reference
remote objects using MVs that reference the remote objects.
This reduces or eliminates the data from network (network round trips), which is a costly
operation.
Note: Because the constraint information of the remote tables is not available at the remote site,
query rewrite will not make use of any constraint information.
The materialized view shown in the first example above is present at the local site, but it
references tables that are all found at the remote site.
The query in the second example shown above contains tables that are found at a single remote
site.
Even though the query references remote tables, it will be rewritten using the above materialized
view as shown in the third example above.
Summary
Objectives
phone_numbers
nls_language
PRODUCT_ nls_territory
INFORMATION PRODUCT_ credit_limit
product_id DESCRIPTION cust_ email
product_name product_id account_mgr_id
product_description language_id date_of_birth
product_name marital_status
category_id
product_description gender
weight_class
Income_level
warranty_period
supplier_id
product_status
list_price INVENTORIES WAREHOUSES
min_price product_id warehouse_id
catalog_url warehouse_id warehouse_name
quantity_on_hand 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 company sells several categories of products, including computer hardware and software,
music, clothing, and tools. The company maintains product information that includes product
identification numbers, the category into which the product falls, the weight group (for shipping
purposes), the warranty period if applicable, the supplier, the status of the product, a list price, a
minimum price at which a product will be sold, and a URL for manufacturer information.
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.
PRODUCTS CUSTOMERS
prod_id cust_id
prod_name cust_first_name
prod_desc cust_last_name
prod_subcategory cust_gender
prod_subcategory_id cust_year_of_birth
prod_subcategory_desc cust_marital_status COUNTRIES
prod_category cust_street_address country_id
prod_category_id cust_postal_code country_iso_code
prod_category_desc cust_city country_name
prod_weight_class cust_city_id country_subregion
prod_unit_of_measure cust_state_province country_subregion_id
prod_pack_size cust_state_province_id country_region
supplier_id country_id country_region_id
prod_status cust_main_phone_number country_total
prod_list_price cust_income_level country_total_id
prod_min_price cust_credit_limit Country_name_hist
prod_total cust_email
prod_total_id cust_total
prod_src_id cust_total_id
prod_eff_from cust_src_id
prod_eff_to cust_eff_from
prod_valid cust_eff_to
cust_valid
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.
PIVOT UNPIVOT
Note: An UNPIVOT does not reverse a PIVOT operation; instead, it rotates data from columns
into rows.
PRODUCT Q1 Q2 Q3 Q4
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.
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.
In this lesson, you use the newly created view, sales_view. The definition of sales_view
is displayed as follows:
DESCRIBE sales_view
Name Null? Type
-------------------------- -------- ------------------
PRODUCT NOT NULL VARCHAR2(50)
COUNTRY NOT NULL VARCHAR2(40)
CHANNEL NOT NULL NUMBER
QUARTER VARCHAR2(8)
AMOUNT_SOLD NUMBER
QUANTITY_SOLD NUMBER
The calendar_quarter_desc column values in the SH schema are as follows:
SELECT DISTINCT calendar_quarter_desc
FROM times;
CALENDAR_QUARTER_DESC
---------------------
1999-02
2000-04
2002-03
2000-03
2001-04
1998-01
20 rows selected.
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.
The two boxes around QUARTER and “9502 rows selected” displayed in the second
example above, highlight the two key changes performed by the PIVOT operator:
• The QUARTER column becomes multiple columns, each holding one quarter.
• The row count will drop to just 71 from 9502, representing the distinct products in the
schema.
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.
SELECT DISTINCT channel_id, channel_desc
FROM channels
ORDER BY channel_id;
CHANNEL_ID CHANNEL_DESC
---------- --------------------
2 Partners
3 Direct Sales
4 Internet
5 Catalog
9 Tele Sales
COLUMN QUARTER FORMAT A7
The example above uses a subquery (inline view) in the FROM clause. This is needed because if
you issue a SELECT * directly from the sales view, the query outputs rows for each row of
sales_view.
The result set shows the product column followed by a column for each value of quarter specified
in the IN clause. The numbers shown in the pivoted output are the sum of quantity_sold
for each product at each quarter. If you also specify an alias for each measure, then the column
name is a concatenation of the pivot column value or alias, an underscore (_), and the measure
alias. You can use aliases in the above example as shown below:
SELECT *
FROM (SELECT product, quarter, quantity_sold
FROM sales_view) PIVOT (sum(quantity_sold)
FOR quarter in ('01' AS Q1, '02' AS Q2, '03' AS Q3,
'04' AS Q4))
ORDER by PRODUCT DESC;
PRODUCT Q1 Q2 Q3 Q4
---------------- ---------- ---------- ---------- ----------
Y Box 1455 1766 1716 1992
Xtend Memory 3146 4121 4122
Unix/Windows 4259 3887 4601 4049
Standard Mouse 3376 1699 2654 2427
The example above uses the Order Enter (OE) schema. The oe.orders table contains
information about when an order was placed (order_date), how it was placed
(order_mode), and the total amount of the order (order_total), as well as other
information. The example shows how to use the PIVOT clause to pivot order_mode values
into columns, aggregating order_total data in the process, to get yearly totals by order mode.
The example creates a new table, pivot_table, using a subquery. The example, creates the
new table, inserts rows returned by the subquery, and uses aliases for the direct and online
pivot column values.
SELECT *
FROM
(SELECT product, channel, quarter, quantity_sold
FROM sales_view) PIVOT (sum(quantity_sold) FOR (channel,
quarter) IN ((3, '01') AS Direct_Sales_Q1,
(4, '01') AS Internet_Sales_Q1))
ORDER BY product DESC;
The example above pivots on both the CHANNEL and QUARTER columns. The example uses only
the CHANNEL values 3 (Direct Sales) and 4 (Internet) and only the Q1 value for the
QUARTER column. The example below specifies more values for the QUARTER column:
SELECT *
FROM
(SELECT product, channel, quarter, quantity_sold
FROM sales_view
) PIVOT (sum(quantity_sold) FOR (channel, quarter) IN
((3, '01') AS Direct_Sales_Q1,
(3, '02') AS Direct_Sales_Q2,
(3, '03') AS Direct_Sales_Q3,
(3, '04') AS Direct_Sales_Q4,
(4, '01') AS Internet_Sales_Q1,
(4, '02') AS Internet_Sales_Q2,
(4, '03') AS Internet_Sales_Q3,
(4, '04') AS Internet_Sales_Q4))
ORDER BY product DESC;
SELECT *
FROM
(SELECT product, channel, amount_sold, quantity_sold
FROM sales_view) PIVOT (SUM(amount_sold) AS sums,
SUM(quantity_sold) as sumq
FOR channel IN (3 AS Dir_Sales, 4 AS Int_Sales))
ORDER BY product DESC;
71 rows selected.
The query in the example above pivots SALES_VIEW on the CHANNEL column. The
amount_sold and quantity_sold measures get pivoted.
Note that the query creates column headings by concatenating the pivot columns with the aliases
of the aggregate functions, plus an underscore.
When you use multiple aggregations, you can omit the alias for only one aggregation. If you omit
an alias, then the corresponding result column name is the pivot value (or the alias for the pivot
value).
Distinguishing PIVOT-Generated
Nulls from Nulls in the Source Data
SELECT * FROM sales2;
SELECT *
FROM
(SELECT prod_id, qtr, amount_sold
FROM sales2) PIVOT (SUM(amount_sold), COUNT(*) AS count_total
FOR qtr IN ('Q1', 'Q2') )
ORDER BY prod_id DESC;
PROD_ID Q1 Q1_COUNT_TOTAL Q2 Q2_COUNT_TOTAL
------- --- -------------- --------- ----------------
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. The following example illustrates NULL that PIVOT generates.
The first code example above, assumes an existing table named sales2. 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,
qtr VARCHAR2(5),
amount_sold NUMBER);
INSERT INTO sales2 VALUES(100, 'Q1', 10);
INSERT INTO sales2 VALUES(100, 'Q1', 20);
INSERT INTO sales2 VALUES(100, 'Q2', null);
INSERT INTO sales2 VALUES(200, 'Q1', 50);
The query in the second code example returns rows prod_id and the resulting pivot columns:
Q1, Q1_COUNT_TOTAL, Q2, Q2_COUNT_TOTAL.
For each unique value of prod_id, Q1_COUNT_TOTAL, the query returns the total number of
rows whose QTR value is Q1. The unique value of Q2_COUNT_TOTAL returns the total number
of rows whose QTR value is Q2.
From the result set shown in the second example above, there are two sales rows for prod_id
L
XM
The XML string includes all
If you use the pivot values found by the
XML keyword subquery even if there are
with the Pivot no aggregate values.
syntax
Use a subquery
Using the XML keyword in the Pivot syntax requires including either the ANY keyword or a
subquery.
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. The values of the pivot column are evaluated at execution time.
PRODUCT
--------------------------------------------------
CHANNEL_XML
------------------------------------------------------------------------
. . .
1.44MB External 3.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.
The ANY keyword acts as a wildcard. If you specify ANY, all values found in the pivot column
will be used for pivoting. Note that when using the ANY keyword, 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 XML output includes all channel values
in the sales_view view created earlier in this lesson. The ANY keyword is only available in
PIVOT operations as part of an XML operation. This output includes data for cases where the
channel exists in the data set. You can use wildcards or subqueries to specify the pivot IN list
members when the values of the pivot column are not known.
PRODUCT
----------
CHANNEL_XML
-----------------------------------------------------------------------
. . .
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>
. . .
The example above illustrates the use of a subquery with the XML keyword.The XML output
includes all channel values and the sales data corresponding to each channel and for each product.
Subquery-based pivots give results different from those generated by the use of the ANY wildcard.
In the example above, when you use a subquery, 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. For example, 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, therefore, often have longer output than queries based on the
ANY keyword. Depending on how you process the query results, subquery-style output may be
more convenient to work with than the results derived from ANY.
Note: The results in the above example do not show a complete row of output due to space
limitations.
Shorts Q2 3,500
Shorts Q3 2,000
The example above unpivots the QUARTER column, thus turning the quarters columns into the
values of the QUARTER column.
An UNPIVOT operation does not reverse a PIVOT operation; instead, it rotates data found in
multiple columns of a single row into multiple rows of a single column. If you are working with
pivoted data, an UNPIVOT operation cannot reverse any aggregations that have been made by
PIVOT or any other means.
Note that the first example contains two rows before the unpivoting operation. In the second
example, the unpivoting operation on the QUARTER column displays five rows. Unpivoting
transforms fewer rows of input into generally more rows.
UNPIVOT
Data from sources such as spreadsheets and flat files is often in pivoted form. For instance, sales
data will often be stored in a separate column for each time period. UNPIVOT can normalize
such data, transforming multiple columns into a single column.
When the data is normalized with UNPIVOT, it is much more accessible for relational database
processing with SQL. By placing data in a normalized layout, queries can readily apply SQL
aggregate and analytic functions, enabling powerful analysis. Similarly, it is more efficient to
specify WHERE clause predicates on normalized data.
The unpivot operation turns a set of value columns into one column. Therefore, the data types of
all the value columns must be of the same data type such as numeric or character.
• If all the value columns are CHAR, then the unpivoted column is CHAR.
• If any value column is VARCHAR2, then the unpivoted column is VARCHAR2.
• If all the value columns are NUMBER, then the unpivoted column is NUMBER.
• If any value column is BINARY_DOUBLE, then the unpivoted column is
BINARY_DOUBLE.
• If no value column is BINARY_DOUBLE but any value column is BINARY_FLOAT, then
the unpivoted column is BINARY_FLOAT.
unpivot_for_clause =
FOR { column | ( column [, column]... ) }
unpivot_in_clause =
( { column | ( column [, column]... ) }
[ AS { constant | ( constant [, constant]... ) } ]
[, { column | ( column [, column]... ) }
[ AS { constant | ( constant [, constant]...) } ] ]...)
Table created.
PRODUCT Q1 Q2 Q3 Q4
------------------ ---------- ---------- ---------- ----------
Y Box 1455 1766 1716 1992
Xtend Memory 3146 4121 4122 3802
. . .
71 rows selected.
The QUARTER column in the above example was formatted using the following command:
COLUMN quarter FORMAT A7
Before Oracle Database 11g, you can simulate the UNPIVOT syntax in the above example using
existing SQL as shown below:
SELECT product, 'Q1' as quarter, Q1 as quantity_sold
FROM pivotedTable
WHERE Q1 is not NULL
union all
SELECT product, 'Q2' as quarter, Q2 as quantity_sold
FROM pivotedTable
WHERE Q2 is not NULL
union all
SELECT product, 'Q3' as quarter, Q3 as quantity_sold
FROM pivotedTable
WHERE Q3 is not NULL
union all
SELECT product, 'Q4' as quarter, Q4 as quantity_sold
FROM pivotedTable
WHERE Q4 is not NULL;
However the UNPIVOT syntax enables more efficient query processing. The UNPIVOT keyword
alerts the query optimizer to the desired behavior. As a result, the optimizer calls highly efficient
algorithms.
9 rows selected.
The UNPIVOT clause enables you to restore a pivoted table, or a table with similar structure, so
that selected columns are pivoted into values in a single column.
Note the use of column aliases in the above example.
SELECT *
FROM multi_col_pivot;
The example above creates a pivot table using the CHANNEL and QUARTER columns in the SH
schema. This is similar to the “Pivoting Using Multiple Columns” example mentioned earlier.
The example uses only the CHANNEL values 3 (Direct Sales) and 4 (Internet) and only the Q1
value for the QUARTER column. You can use other values for both columns. The structure of the
newly created table is shown below:
DESCRIBE multi_col_pivot
Name Null? Type
--------------------- -------- ------------------------
PRODUCT NOT NULL VARCHAR2(50)
DIRECT_SALES_Q1 NUMBER
INTERNET_SALES_Q1 NUMBER
The query in the example above returns 71 rows.
SELECT *
FROM multi_col_pivot
UNPIVOT (quantity_sold For (channel, quarter) IN
( Direct_Sales_Q1 AS ('Direct', 'Q1'),
Internet_Sales_Q1 AS ('Internet', 'Q1') ) )
ORDER BY product DESC, quarter;
The example above unpivots the CHANNEL and QUARTER columns using the
multi_col_pivot table created on the previous page. Note that the example uses explicit
values for the unpivoted CHANNEL and QUARTER columns. The example below demonstrates
unpivoting on the CHANNEL and QUARTER columns without using aliases as explicit values for the
unpivoted columns. In this case, each unpivoted column uses the column name as its value.
The query in the example above returns 142 rows.
SELECT *
FROM multi_col_pivot
UNPIVOT (quantity_sold For (channel, quarter) IN
(Direct_Sales_Q1, Internet_Sales_Q1 ) );
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. This is similar to
the “Pivoting Using Multiple Aggregates” example mentioned earlier. The example uses only the
CHANNEL values 3 (Direct Sales) and 4 (Internet). 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, plus an underscore.
When you use multiple aggregations, you can omit the alias for only one aggregation. If you omit
an alias, then the corresponding result column name is the pivot value (or the alias for the pivot
value). The structure of the newly created table is shown below:
DESCRIBE multi_agg_pivot
Name Null? Type
------------------- --------- -------------------------
PRODUCT NOT NULL VARCHAR2(50)
QUARTER VARCHAR2(8)
DIRECT_SUMQ NUMBER
DIRECT_SUMA NUMBER
INTERNET_SUMQ NUMBER
INTERNET_SUMA NUMBER
The Unpivot example shown above uses the newly created multi_agg_pivot table. The
measures amount_sold and quantity_sold are unpivoted. Channels are mapped to the
value “3” for Direct_sumq and Direct_suma and to the value “4” for Internet_sumq
and Internet_suma. The channel mapping is consistent with the values used in the pivot
operation that created the multi_agg_pivot table. However, any values could have been
used for the channel mappings.
Summary