Dynamic Column Generation by using pivot [message #571557] |
Tue, 27 November 2012 01:38 |
|
saipradyumn
Messages: 419 Registered: October 2011 Location: Hyderabad
|
Senior Member |
|
|
Hi Michel,
Is there any way to generate columns dynamically by depending on the rows in a table in 11G .
Ex: If the deptno in DEPT table is not constant,then how to generate the N numbers of columns based on the deptno.
Below query is working when we hard coded the deptno (10,20,30,40).What else if we more number of departments and we don't know the departments also.
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as dbo
SQL> SELECT *
FROM (SELECT deptno, job, sum(sal) sal
FROM SCOTT.emp GROUP BY job, deptno) PIVOT(sum(sal) FOR deptno IN(10,
20,
30,
40));
JOB 10 20 30 40
--------- ---------- ---------- ---------- ----------
CLERK 1300 1900 950
SALESMAN 5600
PRESIDENT 5000
MANAGER 2450 2975 2850
ANALYST 6000
I just tried by the following way.Second query was not working ,where as first one was working fine.
SQL> select *
2 from scott.dept sd
3 where sd.deptno in (select distinct deptno from scott.emp);
DEPTNO DNAME LOC
------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
SQL> SELECT *
2 FROM (SELECT deptno, job, sum(sal) sal F
ROM SCOTT.emp GROUP BY job, deptno) PIVOT(sum(sal) FOR deptno IN (select distinct deptno
3 from scott.emp));
SELECT *
FROM (SELECT deptno, job, sum(sal) sal
FROM SCOTT.emp GROUP BY job, deptno) PIVOT(sum(sal) FOR deptno IN (select distinct deptno from scott.emp ))
ORA-00936: missing expression
Please help me
Thanks in advance
SaiPradyumn
[Updated on: Tue, 27 November 2012 01:45] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|