Обсуждение: Re: PostgreSQL needs percentage function

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

Re: PostgreSQL needs percentage function

От
Michael Nolan
Дата:


On Mon, Dec 18, 2017 at 6:23 AM, Nick Dro <postgresql@walla.co.il> wrote:
Hi,
Why PostgreSQL doesn't have build-in function to calculate percentage? somthing like percent(number,%
for example:
select percent(100,1) will calculate 1% of 100 = 1
select percent(25,20) will calculate 20% of 25 = 5
 
Seems like a nice addition to the math functions list:
 
This is veryhelpull function, many uses percentage calculation in thier work and it will simplify the process. Percentage calculation is considered a basic math operation and I think postgresql should support it as a build-in function.
Seems like easy to implment isn't it?

It's a bit trickier than that, because you';ll have to deal with integers, real, double precision, etc.  You may also want to deal with null values.  I found it more useful to write a function that displays X as a percentage of Y, rounded to 1 digit to the right of the decimal point.
--
Mike Nolan

Re: PostgreSQL needs percentage function

От
Edson Carlos Ericksson Richter
Дата:

Em 18/12/2017 14:28, Michael Nolan escreveu:


On Mon, Dec 18, 2017 at 6:23 AM, Nick Dro <postgresql@walla.co.il> wrote:
Hi,
Why PostgreSQL doesn't have build-in function to calculate percentage? somthing like percent(number,%
for example:
select percent(100,1) will calculate 1% of 100 = 1
select percent(25,20) will calculate 20% of 25 = 5
 
Seems like a nice addition to the math functions list:
 
This is veryhelpull function, many uses percentage calculation in thier work and it will simplify the process. Percentage calculation is considered a basic math operation and I think postgresql should support it as a build-in function.
Seems like easy to implment isn't it?

It's a bit trickier than that, because you';ll have to deal with integers, real, double precision, etc.  You may also want to deal with null values.  I found it more useful to write a function that displays X as a percentage of Y, rounded to 1 digit to the right of the decimal point.
--
Mike Nolan

I believe that having a built-in function for percentage is not a good idea. Is just like having a function to calculate fractions... Someone would like to have percentages as 0..1, others would like to have it multiplied by 100. How to deal with integers? And so on (as others already stated here).

But, is it possible to have a aggregate function that calculates de percent from the sum (and/or count) total (as a value from 0 ... 1) for numeric (or double)?

Looking really superficially seems to me that would be a great addition to the aggreggates (and is not just a trivial div/multiply op)


Edson

Re: PostgreSQL needs percentage function

От
"David G. Johnston"
Дата:
On Mon, Dec 18, 2017 at 9:56 AM, Edson Carlos Ericksson Richter <richter@simkorp.com.br> wrote:
But, is it possible to have a aggregate function that calculates de percent from the sum (and/or count) total (as a value from 0 ... 1) for numeric (or double)?

​Do you mean:

SELECT id, val, val / (sum(val) OVER ())
FROM vals;​

​David J.​

Re: PostgreSQL needs percentage function

От
Corey Taylor
Дата:
On Mon, Dec 18, 2017 at 11:01 AM, David G. Johnston <david.g.johnston@gmail.com> wrote:
​Do you mean:

SELECT id, val, val / (sum(val) OVER ())
FROM vals;​


You could end up with a percentage > 100 or divide by 0 with that if the values are not in a proper range.  I don't know if that would be the intention.

Re: PostgreSQL needs percentage function

От
Edson Carlos Ericksson Richter
Дата:

Em 18/12/2017 15:01, David G. Johnston escreveu:
On Mon, Dec 18, 2017 at 9:56 AM, Edson Carlos Ericksson Richter <richter@simkorp.com.br> wrote:
But, is it possible to have a aggregate function that calculates de percent from the sum (and/or count) total (as a value from 0 ... 1) for numeric (or double)?

​Do you mean:

SELECT id, val, val / (sum(val) OVER ())
FROM vals;​

​David J.​


I've never thought about Window Functions that way.
Thanks, it is enligthning.

:-)

Regards,

Edson.