Skip to Main Content

SQL & PL/SQL

pivot report with dynamic names

Balaji_MadhavanNov 24 2020

Hi Team,
I need a pivot kind of report in which i cannot use the employee name hardcoded, below is my query pls help me out
/* Code to execute my query */
create table questions (emp_name varchar2(100),question varchar2(1000),answer varchar2(1000));
Insert into questions values('emp1','how old are you','27');
Insert into questions values('emp2','how old are you','28');
Insert into questions values('emp3','how old are you','29');
Insert into questions values('emp1','what is your fav color','blue');
Insert into questions values('emp2','what is your fav color','black');
Insert into questions values('emp3','what is your fav color','red');
/* end */
SELECT *
FROM questions
PIVOT ( MIN (answer)
FOR emp_name IN ('emp1','emp2'));
Here i dont want to hardcode emp1 and emp2 it need to be generated dynamically, I have tried using sub query in IN condition but it dint work . My output should be like the below image
image.pngwe have so many employees this is a sample data

Comments
Post Details
Added on Nov 24 2020
1 comment
34 views