Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL for 'Crosstab-Query'
In article <38E5D97E.89B7FBE0_at_t-online.de>,
Edgar Rohr <ERohrPyro_at_t-online.de> wrote:
> Hi all,
>
> Could somebody send me a typical SQL-String to create a Crosstab-Query
> on a Oracle 7.3-DB
>
> Thx for your help!
>
scott_at_8i> REM First, we need to know the jobs we will pivot on --
> REM this just shows us what they are:
scott_at_8i>
scott_at_8i> select distinct job from emp;
JOB
scott_at_8i> scott_at_8i> scott_at_8i> REM then we can see how to build a sparse matrix in one pass scott_at_8i> REM that moves the sal column into the right virtual column scott_at_8i> scott_at_8i> select deptno, decode( job, 'ANALYST', sal, 0 ) Analyst, 2 decode( job, 'CLERK', sal, 0 ) Clerk, 3 decode( job, 'MANAGER', sal, 0 ) Manager,4 decode( job, 'PRESIDENT', sal, 0 ) President, 5 decode( job, 'SALESMAN', sal, 0 ) Salesman 6 from emp
scott_at_8i> scott_at_8i> REM then we see how sum in this case and group by to 'squash' scott_at_8i> REM the matrix by some grouping value scott_at_8i> scott_at_8i> select deptno, sum( decode( job, 'ANALYST', sal, 0 ) )Analyst,
2 sum( decode( job, 'CLERK', sal, 0 ) ) Clerk, 3 sum( decode( job, 'MANAGER', sal, 0 ) ) Manager, 4 sum( decode( job, 'PRESIDENT', sal, 0 ) ) President, 5 sum( decode( job, 'SALESMAN', sal, 0 ) ) Salesman6 from emp
Sent via Deja.com http://www.deja.com/
Before you buy.
Received on Sat Apr 01 2000 - 07:09:16 CST
![]() |
![]() |