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

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: getting around---division by zero on numeric
Дата
Msg-id 435643A3.9030700@archonet.com
обсуждение исходный текст
Ответ на getting around---division by zero on numeric  (Tim Nelson <timnelson@phreaker.net>)
Ответы Re: getting around---division by zero on numeric  (Berend Tober <btober@seaworthysys.com>)
Список 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

В списке pgsql-general по дате отправления:

Предыдущее
От: Sean Davis
Дата:
Сообщение: Re: getting around---division by zero on numeric
Следующее
От: "Jim Buttafuoco"
Дата:
Сообщение: Re: function that resolves IP addresses