PIVOT Clause

The PIVOT clause is used to cross-tabulation clauses i.e. clauses modifying rows to become.

Syntax:

SELECT * FROM
(
SELECT col1, col2,..., colN
FROM col1, col2,..., colN
[WHERE condition]
)
PIVOT
(
agg_func(col)
FOR col
IN ( expr1, expr2, ... exprN )
)
[ORDER BY expression [ ASC | DESC ]];

Parameters:

  • condition: Optional filtering conditions for the data.
  • agg_func: The aggregation function to use.
  • col: The column whose values are to be used in the pivot clause.
  • expr1, expr2, … exprN: Expressions defining values for the columns used in the pivot.
  • expression: An optional expression used to order the final output data.

We will look at an example in the following sections.

How Efficiently Convert Rows to Columns in PL/SQL?

PL/SQL, or Procedural Language/Structured Query Language, is an extension of SQL used for writing procedural code within Oracle databases. It is a proprietary procedural extension of SQL developed by Oracle Corporation specifically for Oracle Database.

It combines the power of SQL with procedural constructs like loops, conditions, and exception handling. It is a blocked programming language, programming units can be named or unnamed blocks. Unnamed blocks are never stored in the database.

Similar Reads

DECODE Function

The DECODE function in Oracle SQL is a conditional expression that compares an expression (expr) to a specified value (val). If the two match, it returns a specified result (res). It serves as a concise alternative to the if-else statement in procedural languages....

PIVOT Clause

The PIVOT clause is used to cross-tabulation clauses i.e. clauses modifying rows to become....

How Efficiently Convert Rows to Columns in PL/SQL

Efficiently converting rows to columns in PL/SQL is a common database task. Whether using the PIVOT clause or DECODE function statements, this process transforms data structures for improved analysis and reporting....

Setting up the Environment

Let us first set up some environment that can we use to understand the concepts in the following sections. The following query creates a table and inserts some records in it:...

1. Using Decode Function

Earlier we saw what Decode function does. In the following query, we will make use of decode function to convert rows to columns. We group the data by the id column and then use the decode function to return the max of the val2 column for values ‘type1’, ‘type2’, and ‘type3’ in the val1 column....

2. Using Pivot Clause

Earlier we saw what the Pivot clause is. We understood that it can be used to convert rows to columns. In the following query, we do the same. We convert all the rows which have values ‘type1‘, ‘type2‘, or ‘type3‘ in the val1 column to separate columns. We make use of the MAX aggregation function....

Conclusion

In this article, we saw how we can efficiently convert rows to columns in PL/SQL. We started by looking at several functions like DECODE and PIVOT. We understood what they do and saw examples of them. We later saw several methods to find the required values. First, we saw how to use the decode function to convert the row to the column using a hack and then saw the proper use of the PIVOT clause to easily convert rows to columns...

Contact Us