Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to dynamically pivot on unknown column.

syed haiderNov 18 2022 — edited Nov 18 2022

In the Oracle SQL query, how can I pivot without hardcoding the values of leave_reason?
CREATE TABLE Persons (
group_id int,
leave_reason varchar(40),
person_id int,
number_of_days int
);
INSERT INTO Persons (group_id, leave_reason, person_id, number_of_days)
VALUES (1, 'Employee''s PTO', 44, 1);
INSERT INTO Persons (group_id, leave_reason, person_id, number_of_days)
VALUES (2, 'EMPLOYEE''S SERIOUS CONDITION', 55, 5);
INSERT INTO Persons (group_id, leave_reason, person_id, number_of_days)
VALUES (3, 'EMPLOYEE''S SERIOUS CONDITION', 66, 7);
INSERT INTO Persons (group_id, leave_reason, person_id, number_of_days)
VALUES (3, 'Family Vacation', 77, 2);
I want the output as follows;
image.pngPlease kindly let me know how can I write a pivot query to obtain this output without hardcoding the values of LEAVE_REASON field?
I appreciate your help and thank you in advance!

This post has been answered by Frank Kulash on Nov 19 2022
Jump to Answer

Comments

Post Details

Added on Nov 18 2022
9 comments
10,463 views