Search Postgresql Archives

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

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

 



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

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[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