-
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
-
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.
-
select deptno, sal, (sal / sum(sal) over () * 100) final_val from test
group by deptno, sal;
Cheers!
OraKid.
-
Is it possible to do it, without craeting new table, some how within the query?
-
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.
-
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
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|