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

From: Richard Huxton <dev(at)archonet(dot)com>
To: Tim Nelson <timnelson(at)phreaker(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: getting around---division by zero on numeric
Date: 2005-10-19 13:01:23
Message-ID: 435643A3.9030700@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tim Nelson wrote:
> I am getting division by zero on a calculated field ( sum(sales) is 0 )

It's a two-stage process, so you'll want a sub-query. Something like:

SELECT
type,
tot_sales,
tot_cost
((tot_sales * tot_cost / tot_sales) * 100) AS percent
FROM
(
SELECT
type, sum(sales) AS tot_sales, sum(cost) AS tot_cost
FROM
test
GROUP BY
type
HAVING
sum(sales) <> 0
) AS base
;

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2005-10-19 13:07:11 Re: psql runs out of memory
Previous Message Sean Davis 2005-10-19 13:01:12 Re: getting around---division by zero on numeric