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

Thread: Query help

  1. #1
    Join Date
    Dec 2000
    Location
    Virginia, USA
    Posts
    455

    Query help

    Using EMP Table
    SQL> select deptno, sum(sal) from emp
    2 group by deptno;

    DEPTNO SUM(SAL)
    ---------- ----------
    10 8750
    20 10875
    30 9400

    Now How do I get % of total

    DEPTNO SUM(SAL) %
    ---------- ---------- -----
    10 8750 30.14
    20 10875 37.46
    30 9400 32.38

    I want another column with 8750/29025 and so on(which is a grand total) I like to get % of sal at each deptno level.

    Thanks in Advance

  2. #2
    Join Date
    Mar 2001
    Location
    Reading, U.K
    Posts
    598
    try this...

    create table test(deptno number, sal number);
    insert into test values(10, 8750);
    insert into test values(20, 10875);
    insert into test values(30, 9400);
    commit;

    select deptno, sal, sum(sal) over (), sal / sum(sal) over () final_val from test;

    Cheers!
    Cheers!
    OraKid.

  3. #3
    Join Date
    Mar 2001
    Location
    Reading, U.K
    Posts
    598
    select deptno, sal, (sal / sum(sal) over () * 100) final_val from test
    group by deptno, sal;
    Cheers!
    OraKid.

  4. #4
    Join Date
    Dec 2000
    Location
    Virginia, USA
    Posts
    455
    Is it possible to do it, without craeting new table, some how within the query?

  5. #5
    Join Date
    Mar 2001
    Location
    Reading, U.K
    Posts
    598
    Is it possible to do it, without craeting new table, some how within the query? -- i don't understand this...


    since i don't hv EMP table as in ur example I create one called TEST.

    Cheers!
    Cheers!
    OraKid.

  6. #6
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    8

    Wink Re: is it possible to do it, without craeting new table, some how within the query?

    select t1.sub_t_sal*100/t2.grant_t_sal sal_per
    from
    (select deptno, sum(sal) sub_t_sal
    from emp
    group by deptno
    ) t1,
    (select sum(sal) grand_t_sal
    from emp
    ) t2


    since pseudo table t2 is having one row, we need not worry about cartesian product.


    Good luck !!
    Brahmaiah Koniki

  7. #7
    Join Date
    Aug 2003
    Location
    china
    Posts
    7
    select a.deptno,sum(a.sal),a.sal/b.totalsal*100 || '%' from test a,(select sum(sal) totalsal from test) b group by a.deptno,a.sal/b.totalsal

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