calculating percentages

Поиск
Список
Период
Сортировка
Искать

calculating percentages

От:
Thomas Good <tomg@admin.nrnet.org>
Дата:
Hi.

I am trying to do outcome studies on pt before and after treatment
recidivism rates...and having some trouble calculating percentages.

How does one do this:   SELECT (current_value/target) * 100;
in postgres?  

Cheers - and thanks,
Tom

------- North Richmond Community Mental Health Center -------

Thomas Good                                   MIS Coordinator
Vital Signs:                  tomg@ { admin | q8 } .nrnet.org                                         Phone: 718-354-5528                                           Fax:   718-354-5056                               
/* Member: Computer Professionals For Social Responsibility */ 


Re: [SQL] calculating percentages

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Thomas Good  writes:
> How does one do this:   SELECT (current_value/target) * 100;
> in postgres?  

Pretty much just like that.  What problem are you having?
		regards, tom lane

Re: [SQL] calculating percentages

От:
Thomas Good <tomg@nrnet.org>
Дата:
On Mon, 12 Jul 1999, Tom Lane wrote:

> Thomas Good  writes:
> > How does one do this:   SELECT (current_value/target) * 100;
> > in postgres?  

> Pretty much just like that.  What problem are you having?
> 
> 			regards, tom lane

The divison rtns 0, which multiplied by 100, rtns 0.
It's neat.  But not quite the dramatic proof (of the efficacy
of our treatment) that my superiors are looking for!

------- North Richmond Community Mental Health Center -------

Thomas Good                                   MIS Coordinator
Vital Signs:                  tomg@ { admin | q8 } .nrnet.org                                         Phone: 718-354-5528                                           Fax:   718-354-5056                               
/* Member: Computer Professionals For Social Responsibility */ 


Re: [SQL] calculating percentages

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Thomas Good  writes:
> On Mon, 12 Jul 1999, Tom Lane wrote:
>> Pretty much just like that.  What problem are you having?

> The divison rtns 0, which multiplied by 100, rtns 0.

If your data is stored as int fields, then you'd get integer division,
which sounds like what you're getting.  You want a floating-point
division, which you'd get by promoting to float:
SELECT (current_value::float8/target::float8) * 100;
		regards, tom lane

Re: [SQL] calculating percentages

От:
"D'Arcy" "J.M." Cain <darcy@druid.net>
Дата:

Thus spake Thomas Good
> > > How does one do this:   SELECT (current_value/target) * 100;
> The divison rtns 0, which multiplied by 100, rtns 0.

Does "SELECT (current_value * 100)/target" work better?

-- 
D'Arcy J.M. Cain    |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.


Re: [SQL] calculating percentages

От:
Thomas Good <tomg@admin.nrnet.org>
Дата:
On Tue, 13 Jul 1999, D'Arcy J.M. Cain wrote:

> Thus spake Thomas Good
> > > > How does one do this:   SELECT (current_value/target) * 100;
> > The divison rtns 0, which multiplied by 100, rtns 0.
> 
> Does "SELECT (current_value * 100)/target" work better?

Darcy, 

SELECT (2 * 100)/3;             
       ?column?
----------------
66
(1 row)                              

This is clean and I don't have to truncate the value in the end.  
Thanks!  

BTW, I noticed an idiosnycratic behaviour that I don't really
care for when doing floating point calculations:

SELECT (2::float8/3::float8) * 100;
       ?column?
----------------
66.6666666666667    <----------------- I don't need to round up here.
(1 row)                              

EOF

So, Larry W comes in handy again:

#!/usr/bin/perl
$val = (2/3) * 100;
print("$val\n");

66.6666666666666    

------- North Richmond Community Mental Health Center -------

Thomas Good                                   MIS Coordinator
Vital Signs:                  tomg@ { admin | q8 } .nrnet.org                                         Phone: 718-354-5528                                           Fax:   718-354-5056                               
/* Member: Computer Professionals For Social Responsibility */ 


Re: [SQL] calculating percentages

От:
"D'Arcy" "J.M." Cain <darcy@druid.net>
Дата:

Thus spake Thomas Good
> > > How does one do this:   SELECT (current_value/target) * 100;
> The divison rtns 0, which multiplied by 100, rtns 0.

Does "SELECT (current_value * 100)/target" work better?

-- 
D'Arcy J.M. Cain    |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.


FAQ