Beruflich Dokumente
Kultur Dokumente
Pivoting data is one of the most common techniques in BI to present and manipulate data. While doing this using an
ETL or BI tool is usually very simple, doing it in SQL is a more tedious task.
In this article we will see two different ways to pivot data with SQL. The first one is the classical way, while the second
one uses the PIVOT clause available in Oracle 11g.
Scenario
The goal is to get the data in the following table as a data source
Pivoting data is one of the most common techniques in BI used to present and manipulate data. Using an ETL or BI
tool is usually a very simple solution; accomplishing it in SQL is more tedious.
In this article we will see two different ways to pivot data with SQL. The first one is the classical way, while the second
one uses the PIVOT clause available in Oracle 11g.
Scenario
The goal is to get the data in the following table as a data source
and pivot it to get the following results showing the amount by sales, persons and product:
Figure 3: Pivot table structure showing the Rows, Columns and Measures areas
Pivot data in SQL (Classical Query)
The classical way of pivoting data consists of:
Grouping the source data by the columns to be used, which is located in the rows area of the final pivot table
Using CASE/DECODE to build the measure values in the measure area for each value in the columns area
Comparison Analysis
In both approaches, the developer needs to know the different values in advance. Any change in the available values
may require a change in the SQL statement, if we want the new value to be shown in the columns area.
Using the PIVOT clause available in Oracle 11g, you will only need to add the value and the alias in the PIVOT clause
and the alias in the SELECT clause, whereas in the classical approach you will need to rewrite the whole formula.
There is no best syntax to pivot data. Practice using both of these approaches and use the one you feel more
comfortable with as long as your team is comfortable with that syntax. Take into account that some of your colleagues
may be responsible for updating it in the future.