Search Postgresql Archives

Re: Calculating percentages in Postgresql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Peter Nixonn <listuser@xxxxxxxxxxxxxx> writes:

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

One simple way to hack around this is wrap the problematic operand in
'nullif' and let it compute a null result where div by zero is
avoided.

jerry@jerry#
= \pset null 'Oops!'
Null display is "Oops!".
jerry@jerry#
= select 100 / nullif(num, 0) from generate_series(0,3) foo (num);
 ?column? 
----------
    Oops!
      100
       50
       33
(4 rows)

jerry@jerry#
= 

> -- 
> 
> Peter Nixon
> http://www.peternixon.net/
> PGP Key: http://www.peternixon.net/public.asc
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@xxxxxxxxxxxxxx so that your
>        message can get through to the mailing list cleanly
> 

-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     Production Database Administrator
                305 321-1144 (mobil	WWW E-Commerce Consultant


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux