| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.server -> Re: SQL Question - Select clause within Select List
In article <8jduf4$2hi$1_at_nnrp1.deja.com>,
logo_palanisamy_at_hotmail.com wrote:
> I have a SQL question for the table structure given below.
>
> SQL> desc dept
> Name Null? Type
> ----------------------------------------- --------
Here is a possible solution:
set serveroutput on size 1000000
declare
--
-- Get the department name and department id
--
cursor get_dname is
select deptno, rpad(dname,15) dname
from dept
order by deptno;
--
-- Get the employee count by department
--
cursor get_emp_ct (dno in number) is
select count(*) emp_ct
from emp
where deptno = dno;
--
-- Get the employee names by department
--
cursor get_emp (dno in number) is
select ename
from emp
where deptno = dno
--
-- Counter
--
e_ct number:=0;
begin
--
-- Output headers
--
dbms_output.put_line('DNAME EMPLOYEES');
dbms_output.put_line('-------------------------------------------------
---------------');
--
-- Loop through departments
--
for drec in get_dname loop
--
-- Output department name
--
dbms_output.put(drec.dname||' ');
--
-- Get department employee count
--
-- This is a 'trigger' for the commas
-- in the employee list
--
open get_emp_ct(drec.deptno);
fetch get_emp_ct into e_ct;
close get_emp_ct;
--
-- Loop through employees
--
for erec in get_emp(drec.deptno) loop
--
-- If more than one name put a comma
--
if e_ct > 1 then
dbms_output.put(erec.ename||', ');
else
--
-- One name left, or only one name
-- available
--
-- Don't output a comma
--
dbms_output.put_line(erec.ename);
end if;
--
-- Decrement counter for each name
-- output
--
e_ct := e_ct - 1;
end loop;
end loop;
This will return the data formatted as you desire, and it runs fairly quickly. This is not the only solution, just one I came up with on the spur of the moment.
-- David Fitzjarrell Oracle Certified DBA Sent via Deja.com http://www.deja.com/ Before you buy.Received on Thu Jun 29 2000 - 00:00:00 CDT
![]() |
![]() |