Question: How to display one row of several columns into one column of several rows?
Or
Pivoting one row of
several columns into one column of several rows.
Or
How to pivote one row of several columns into one column of several rows.
For Example consider the following screenshot:
Expected Result:
Answer: Following SQL Query can be used to display one row of several columns into one column of several rows:
SELECT
emp_detail,
case
when emp_detail = 'ID' then ID
when emp_detail = 'NAME' then NAME
when emp_detail = 'JOB' then JOB
when emp_detail = 'SALARY' then SALARY
when emp_detail = 'DEPARTMENT' then DEPARTMENT
end as value
FROM
(
SELECT
pivoter.emp_detail,
ID,
NAME,
JOB,
SALARY,
DEPARTMENT
FROM
emp
cross join
(
select 'ID' as emp_detail from dual
UNION ALL
select 'NAME' as emp_detail from dual
UNION ALL
select 'JOB' as emp_detail from dual
UNION ALL
select 'SALARY' as emp_detail from dual
UNION ALL
select 'DEPARTMENT' as emp_detail from dual
)pivoter
);
Let us know if you face any problem in the query.
COMMENTS