@wilmer.lemke
One efficient way to transform a narrow table into a wide table in Oracle is to use the PIVOT function. Here are the steps to achieve this:
- Identify the columns that you want to pivot. These columns will become the new wide columns in the table.
- Use the PIVOT function along with an aggregate function (such as SUM, MAX, MIN, etc.) to pivot the data. The syntax for the PIVOT function is as follows:
1
2
3
4
5
6
|
SELECT *
FROM narrow_table
PIVOT (
aggregate_function(column_to_pivot)
FOR pivot_column IN (value1, value2, value3, ...)
);
|
- Replace "narrow_table" with the name of your narrow table, "aggregate_function" with the desired aggregate function (e.g. SUM, MAX, MIN), "column_to_pivot" with the column you want to pivot, and "value1, value2, value3, ..." with the values you want to pivot.
- Execute the query to pivot the data and create the wide table.
- You can also use the UNPIVOT function to reverse the pivot operation if needed.
By following these steps and using the PIVOT function in Oracle, you can efficiently transform a narrow table into a wide table.