PIVOT Function is used in oracle 11g to produce query output in matrix style same as what we are doing in xls or xlsx.
in oracle 10g,
select deptid,jobid,sum(sal) from emp
group by deptid,jobid
order by deptno,jobid;
for to get ouput of a query like in Excel Pivot funcationality.
select deptid,
sum(decode(jobid, 101, sal)) "Analyst",
sum(decode(jobid, 102, sal)) "Programmer",
sum(decode(jobid, 103, sal)) "DBA",
sum(decode(jobid, 104, sal)) "Developer"
from emp
group by deptid
order by deptid;
Using the same output in oracle 11g with below query.
select * from(select deptid,jobid,sal from emp)
PIVOT (sum(sal) FOR jobid
in (
101 "Analyst",
102, "Programmer",
103, "DBA",
104, "Developer"
))order by deptid;
same way we can achieve the first query output using UNPIVOT function.
select * from(select deptid,jobid,sal from emp)
UNPIVOT (sum(sal) FOR jobid
in (
101, "Analyst",
102, "Programmer",
103, "DBA",
104, "Developer",
))order by deptid;
in oracle 10g,
select deptid,jobid,sum(sal) from emp
group by deptid,jobid
order by deptno,jobid;
for to get ouput of a query like in Excel Pivot funcationality.
select deptid,
sum(decode(jobid, 101, sal)) "Analyst",
sum(decode(jobid, 102, sal)) "Programmer",
sum(decode(jobid, 103, sal)) "DBA",
sum(decode(jobid, 104, sal)) "Developer"
from emp
group by deptid
order by deptid;
Using the same output in oracle 11g with below query.
select * from(select deptid,jobid,sal from emp)
PIVOT (sum(sal) FOR jobid
in (
101 "Analyst",
102, "Programmer",
103, "DBA",
104, "Developer"
))order by deptid;
same way we can achieve the first query output using UNPIVOT function.
select * from(select deptid,jobid,sal from emp)
UNPIVOT (sum(sal) FOR jobid
in (
101, "Analyst",
102, "Programmer",
103, "DBA",
104, "Developer",
))order by deptid;