Home » RDBMS Server » Server Administration » Re: Try this SQL/PLSQL problem again...
Re: Try this SQL/PLSQL problem again... [message #374453] Wed, 13 June 2001 06:29
Hans
Messages: 42
Registered: September 2000
Member
Try this solution

 
 drop table bosou;
 create table bosou (
   id       number(10),
   posn     varchar2(15),
   eff_date date,
   job_type varchar2(20)
 );
 

              

              
 
insert into bosou (id, posn, eff_date, job_type )
   values ('123456','posn001',to_date('01-JAN-2001','DD-MON-YYYY'),'Primary');
insert into bosou (id, posn, eff_date, job_type )
   values ('123456','posn002',to_date('02-MAR-2001','DD-MON-YYYY'),'Secondary');
insert into bosou (id, posn, eff_date, job_type )
   values ('123456','posn003',to_date('24-MAR-2001','DD-MON-YYYY'),'Secondary');
insert into bosou (id, posn, eff_date, job_type )
   values ('123456','posn007',to_date('03-MAY-2001','DD-MON-YYYY'),'Secondary');
insert into bosou (id, posn, eff_date, job_type )
   values ('231234','posn007',to_date('01-APR-2001','DD-MON-YYYY'),'Primary');
insert into bosou (id, posn, eff_date, job_type )
   values ('909090','posn004',to_date('19-APR-2001','DD-MON-YYYY'),'Primary');
insert into bosou (id, posn, eff_date, job_type )
   values ('909090','posn007',to_date('22-APR-2001','DD-MON-YYYY'),'Secondary');
   

              

              

              

set serveroutput on size 100000
declare
   cursor cur_maxcols is
      select max(numrows) from (
         select count(id) numrows from bosou
            group by id
            );          

              
   cursor cur_outer is
      select id from bosou
         group by id;
         

              
   cursor cur_inner(v_id number) is
      select posn from bosou
      where id = v_id
      order by decode(job_type,'Primary',1,2);

              
   maxcols  number(3);

     
   line     varchar2(255);
   n        number(3);
begin
   open cur_maxcols;
   fetch cur_maxcols into maxcols;
   close cur_maxcols;

  
   dbms_output.put_line( lpad('-',(maxcols+1)*10, '-') );

  
   line := rpad('ID',10,' ');
   for i in 1..maxcols loop
      line := line || rpad('Posn'||i,10,' ');
   end loop;
   dbms_output.put_line( line );

    
   dbms_output.put_line( lpad('-',(maxcols+1)*10, '-') );

     
   for rec_outer in cur_outer loop
      line := rpad(rec_outer.id,10,' ');

     
      n := 0;
      for rec_inner in cur_inner(rec_outer.id) loop
         n := n + 1; 
         line := line || rpad(rec_inner.posn, 10,' ');         
      end loop;
      for i in (n+1)..maxcols loop
         line := line || rpad('NONE', 10,' ');
      end loop;

     
      dbms_output.put_line( line );
   end loop;

  
   dbms_output.put_line( lpad('-',(maxcols+1)*10, '-') );
end;
/

Output:
--------------------------------------------------
ID        Posn1     Posn2     Posn3     Posn4
--------------------------------------------------
123456    posn001   posn002   posn003   posn007
231234    posn007   NONE      NONE      NONE
909090    posn004   posn007   NONE      NONE
--------------------------------------------------

Previous Topic: Sequence error??
Next Topic: Oracle 8.1.7.0 under Sun Solaris 2.6
Goto Forum:
  


Current Time: Fri Jul 05 15:46:33 CDT 2024