DBAsupport.com Forums - Powered by vBulletin
Results 1 to 10 of 10

Thread: rollup with pl sql

  1. #1
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    hi

    does anyone know how to perform group by rollup() with pl sql? It´s because we are using 7.3.4 and cant use this feature

    something like

    select job, deptno, sum(sal)
    from emp
    group by rollup(job,deptno)

  2. #2
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    well, no similar features is available ... you could use breaks and computes, but it's just a SQL*Plus feature, and it won't give you exactly what you want ...
    anyway it should be possible to write a rollup-like function in PL/SQL, but it won't be easy to do so :/

  3. #3
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    cant use sqlplus because it´s gonna run from an application

    so is it tricky :/

  4. #4
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    I think you could quite easily build a PL/SQL function doing the job specifically for the tables you'll use, but if you need a generic script that can do the work for any table on any number of columns, then I doubt it will be possible to do :/

    really tricky :|

  5. #5
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    hmm how would it be just for a single table :-? for example the emp table

  6. #6
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    I'm working on it
    C U in 10-15 minutes with a solution !

  7. #7
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    i am trying too

  8. #8
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    set serveroutput on

    declare

    cursor my_curs is
    select job, deptno, sum(sal) S
    from emp
    group by job, deptno;

    tmp_sum number := 0;
    tmp_job varchar2(9) := '';
    global_sum number := 0;

    begin
    dbms_output.enable(100000);
    for var_curs in my_curs
    loop
    if (var_curs.job != tmp_job and tmp_job IS NOT NULL) then
    dbms_output.put_line(rpad(tmp_job,15,' ') || ' ' || tmp_sum);
    tmp_job := var_curs.job;
    tmp_sum := var_curs.S;
    dbms_output.put_line(rpad(tmp_job,11,' ') || ' ' || var_curs.deptno || ' ' || var_curs.S);
    global_sum := global_sum + var_curs.S;
    else
    tmp_job := var_curs.job;
    tmp_sum := tmp_sum + var_curs.S;
    global_sum := global_sum + var_curs.S;
    dbms_output.put_line(rpad(tmp_job,11,' ') || ' ' || var_curs.deptno || ' ' || var_curs.S);
    end if;
    end loop;
    dbms_output.put_line(rpad('total = ',17,' ') || global_sum);
    end;
    /

  9. #9
    Join Date
    Aug 2001
    Posts
    134

    Hi
    What is purpose of ROLLUP, and Why data shown in repeated format ?

    select deptno,job, sum(sal)
    from emp
    where deptno = 20
    group by rollup(job,deptno)
    /

    DEPTNO JOB SUM(SAL)
    ---- --------- ---------
    20 ANALYST 6000
    ANALYST 6000
    20 CLERK 1100
    CLERK 1100
    20 CLERK. 800
    CLERK. 800
    20 MANAGER 2975
    MANAGER 2975
    20 S D 6500
    S D 6500
    20 s e 10000
    s e 10000

    27375


    Thanx

  10. #10
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    rollup makes partial sums, it's very useful, but you rexample isn't good since you group by job, deptno, with deptno fixed to a value ... just do :

    select deptno,job, sum(sal)
    from emp
    group by rollup(job,deptno)


    and you'll see the usefulness of rollup


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width