What is the function of the pivot function in Oracle?

The PIVOT function in Oracle is used to transform row data into column data. It aggregates row data based on specified column values and displays the aggregated result in a column format.

Specifically, the PIVOT function can group row data in a table according to a specified column and then transform these grouped row data into column data. Typically, the PIVOT function needs to be used in conjunction with an aggregate function to aggregate the data during the transformation process.

Here is the syntax for the PIVOT function:

SELECT * FROM (
  SELECT <select_list>, <pivot_column>, <value_column>
  FROM <table_name>
)
PIVOT (
  <aggregate_function>(<value_column>)
  FOR <pivot_column>
  IN (<value_list>)
)

Here, refers to aggregate functions (such as SUM, COUNT, AVG, etc.), represents the list of columns to be queried, is the column used for column transformation, is the column on which the aggregate operation is to be performed, is the name of the table to be queried, and represents the list of values for column transformation.

By using the PIVOT function, it is easy to convert row data into column data, making it more visually appealing in query results.

bannerAds