Re: getting around---division by zero on numeric

From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: Tim Nelson <timnelson(at)phreaker(dot)net>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: getting around---division by zero on numeric
Date: 2005-10-19 13:01:12
Message-ID: BF7BBBD8.10FFD%sdavis2@mail.nih.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/19/05 8:26 AM, "Tim Nelson" <timnelson(at)phreaker(dot)net> wrote:

> I am getting division by zero on a calculated field ( sum(sales) is 0 )
> and I can't find a way around this. I figured out you can't use an
> aggregate in a where, and using having the parser must (obviously)
> evaluate the select fields before considering teh having clause.
>
> Does anyone have a way around this? Thanks!
>
> select
> type,
> sum(sales),
> sum(cost),
> (sum(sales) * sum(cost) / sum(sales)) * 100
> from test
> group by 1
> having sum(sales) != 0

Can you use case?

http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html

Or you could create a simple function to do the logic to avoid the division
by zero.

Sean

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2005-10-19 13:01:23 Re: getting around---division by zero on numeric
Previous Message Richard Huxton 2005-10-19 12:58:46 Re: Create GLOBAL TABLE