Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: SQL Question

Re: SQL Question

From: Jared Still <jared_at_SIERRA.VALLEYNET.COM>
Date: Thu, 11 Jan 1996 07:35:29 -0800
Message-Id: <9601111557.AA12618@alice.jcc.com>


>
> 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

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US