How can one achieve pivot columns into rows in Oracle?

In Oracle database, you can use the UNPIVOT operation to convert columns into rows. Here is an example:

Assume there is the following table t1:

1. 1, Tom, 25, Paris
2. 2, Jane, 30, Tokyo
3. 3, Alex, 35, London

To convert columns into rows, you can use the UNPIVOT operation as shown below:

SELECT *
FROM t1
UNPIVOT (Value FOR Attribute IN (Name, Age, City))

After running the above query, the following results will be obtained:

ID
Characteristics
Value

1
Appellation
Tom

1
Years old
25

1
Location
Paris

2
Appellation
Jane

2
Years old
30

2
Location
Tokyo

3
Appellation
Alex

3
Years old
35

3
Location
London

By using the UNPIVOT operation, every original row is transformed into multiple rows, with each row containing the ID, attribute, and value of the original row.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds