Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: SQL Question
>
> Let's say I have a table with two columns: dept#, and number of employees
> in each department. The table looks as follow:
> Dept# Number of Employees
> 10 60
> 20 70
> 30 80
> I would like to create a query that returns the percentage the
> number of employees in each department represents of the total
> number of employees. The result will look like:
> Dept# %
> 10 28.57
> 20 33.33
> 30 38.09
> Any help would be appreciated.
> Thx. Iraida
>
A pure SQL solution would be messy and convoluted.
Try this:
assumes table name is x, you may want to change it for production ;)
var emptotal number;
begin
select sum(emp) into :emptotal from x;
end;
/
col emppct format a10
select
dept,
to_char(( emp / :emptotal ) * 100,'99.99') || '%' emppct
from x
order by dept
/
I tested it, and it does what you want.
Jared
jared_at_valleynet.com
Received on Thu Jan 11 1996 - 10:57:43 CST
![]() |
![]() |