Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eQyj0-0000cz-16 for pgsql-general@arkaria.postgresql.org; Mon, 18 Dec 2017 16:57:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eQyiz-00055o-Kp for pgsql-general@arkaria.postgresql.org; Mon, 18 Dec 2017 16:57:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eQyiz-00055e-Dp for pgsql-general@lists.postgresql.org; Mon, 18 Dec 2017 16:57:09 +0000 Received: from server5.simfrete.com ([177.52.183.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eQyin-00046i-7x for pgsql-general@lists.postgresql.org; Mon, 18 Dec 2017 16:57:07 +0000 Received: from [192.168.10.30] (unknown [177.52.183.17]) by server5.simfrete.com (Postfix) with ESMTPSA id A8803240D6A for ; Mon, 18 Dec 2017 14:59:54 -0200 (-02) Subject: Re: PostgreSQL needs percentage function To: pgsql-general@lists.postgresql.org References: <^75EF6A639D2C0E83BEC677FB95E668672BBAB816@walla.co.il> From: Edson Carlos Ericksson Richter Message-ID: <1793b3b5-0a0f-d55e-c546-e5b00899687c@simkorp.com.br> Date: Mon, 18 Dec 2017 14:56:49 -0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------979E9B8C99FB548AAEF7A57A" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------979E9B8C99FB548AAEF7A57A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Em 18/12/2017 14:28, Michael Nolan escreveu: > > > On Mon, Dec 18, 2017 at 6:23 AM, Nick Dro > 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: > https://www.postgresql.org/docs/9.5/static/functions-math.html > > 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 --------------979E9B8C99FB548AAEF7A57A Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

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