Re: division by zero error in a request

Поиск
Список
Период
Сортировка
От Oisin Glynn
Тема Re: division by zero error in a request
Дата
Msg-id 4537CDA7.1010107@oisinglynn.com
обсуждение исходный текст
Ответ на division by zero error in a request  (Bernard Grosperrin <bernard@bgsoftfactory.com>)
Список pgsql-general
Bernard Grosperrin wrote:
> I wants to make a view giving me some statistics.
>
> I am not sure to understand why something like this
>
> SELECT location_id, (sold_parts_amount_dly + sold_labor_amount_dly) /
> (sold_parts_amount_dly + sold_labor_amount_dly) from sales
>
> give me a division by zero error?
>
> If that is not the way to go, should I write a function that I would call
> instead?
>
> Thanks,
> Bernard
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>
If (sold_parts_amount_dly + sold_labor_amount_dly) equals zero you will
get an error as you cannot divide by zero.

Also in every case where (sold_parts_amount_dly + sold_labor_amount_dly)
is not zero wont the answer be 1 as

(sold_parts_amount_dly + sold_labor_amount_dly)/(sold_parts_amount_dly + sold_labor_amount_dly) is always 1??

To try and find the zero you could do the following:

select count(*) from sales where (sold_parts_amount_dly +
sold_labor_amount_dly)=0;

and if that is not a staggering amount of rows
select * from sales where (sold_parts_amount_dly + sold_labor_amount_dly)=0;
and try and identify why this is happening if it is not expected?

or if you want to ignore rows where (sold_parts_amount_dly +
sold_labor_amount_dly)=0;
then

SELECT location_id, (sold_parts_amount_dly + sold_labor_amount_dly) /
(sold_parts_amount_dly + sold_labor_amount_dly) from sales
where (sold_parts_amount_dly + sold_labor_amount_dly)>0

Oisin



Вложения

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

Предыдущее
От: Richard Broersma Jr
Дата:
Сообщение: Re: Help needed
Следующее
От: "Peter Bauer"
Дата:
Сообщение: Re: Overload after some minutes, please help!