Re: Calculating percentages in Postgresql

From: Richard Huxton <dev(at)archonet(dot)com>
To: Peter Nixonn <listuser(at)peternixon(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Calculating percentages in Postgresql
Date: 2006-11-21 14:22:59
Message-ID: 45630BC3.4070000@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Peter Nixonn wrote:
> Hi Guys
>
>
> Has anyone written a postgres function to calculate percentages without
> giving "ERROR: division by zero" when fed zeros?

Almost certainly. The question is, what do *you* want it to do?

You'll want to customise something like:

CREATE FUNCTION percent_plus(int4, int4) RETURNS int4 AS $$
SELECT CASE WHEN $2=0 THEN -1 ELSE ($1*100)/$2 END;
$$ LANGUAGE SQL;

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2006-11-21 14:32:22 Re: Calculating percentages in Postgresql
Previous Message Wm.A.Stafford 2006-11-21 14:20:07 Thanks for all the help on replacing 'unique'