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 Andreas Kretschmer <akretschmer@spamfence.net>
Re: getting around---division by zero on numeric Tom Lane <tgl@sss.pgh.pa.us>
Re: getting around---division by zero on numeric Richard Huxton <dev@archonet.com>
Re: getting around---division by zero on numeric Berend Tober <btober@seaworthysys.com>
Re: getting around---division by zero on numeric Michael Glaesemann <grzm@myrealbox.com>
Re: getting around---division by zero on numeric Sean Davis <sdavis2@mail.nih.gov>
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
Дата:
От: Jim Buttafuoco
Дата:
FAQ