division by zero issue - Mailing list pgsql-general

From Greg Donald
Subject division by zero issue
Date
Msg-id ea9da26c0409150930176a5a55@mail.gmail.com
Whole thread Raw
Responses Re: division by zero issue
Re: division by zero issue
Re: division by zero issue
List pgsql-general
Converting some MySQL code to work with Postgres here.

I have this query:

SELECT
  tasks.task_id,
  (tasks.task_duration * tasks.task_duration_type /
count(user_tasks.task_id)) as hours_allocated
FROM tasks
LEFT JOIN user_tasks
  ON tasks.task_id = user_tasks.task_id
WHERE tasks.task_milestone = '0'
GROUP BY
  tasks.task_id,
  task_duration,
  task_duration_type
;

The problem is that sometimes count(user_tasks.task_id) equals zero,
so I get the division by zero error.  Is there a simple way to make
that part of the query fail silently and just equal zero instead of
dividing and producing the error?

TIA..

--
Greg Donald
http://gdconsultants.com/
http://destiney.com/

pgsql-general by date:

Previous
From: Steve Atkins
Date:
Subject: Converting varchar() to text
Next
From: Jean-Luc Lachance
Date:
Subject: Re: division by zero issue