Re: [professionel] Re: division by zero error in a request

Поиск
Список
Период
Сортировка
От Oisin Glynn
Тема Re: [professionel] Re: division by zero error in a request
Дата
Msg-id 4538D936.2090309@oisinglynn.com
обсуждение исходный текст
Ответ на division by zero error in a request  (Bernard Grosperrin <bernard@bgsoftfactory.com>)
Список pgsql-general
Bernard Grosperrin wrote:
> Oisin
>> 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
> Thanks for your answer.
>
> The real request would be something like this:
>
> SELECT location_id, (sold_parts_amount_dly + sold_labor_amount_dly) -
> cost_amount_dly /
> (sold_parts_amount_dly + sold_labor_amount_dly) from sales where
> (sold_parts_amount_dly + sold_labor_amount_dly)>0
>
> My problem is that in fact I want to SUM those amounts, with a GROUP
> BY per location. But by doing so, any location where one row has where
> = 0 is eliminated. So, is there a way to SUM inside a subset returned by
>
> SELECT location_id, (sold_parts_amount_dly + sold_labor_amount_dly) -
> cost_amount_dly /
> (sold_parts_amount_dly + sold_labor_amount_dly) from sales where
> (sold_parts_amount_dly + sold_labor_amount_dly)>0
>
> ????
>
> Should I select into a temporary table, then SUM that table?
>
> Thanks,
> Bernard
>
Please always copy the list on your responses as others may be interested.
Something like this should work. I am not sure how inefficient it is.

select location_id, SUM((sold_parts_amount_dly + sold_labor_amount_dly)
- cost_amount_dly) /
SUM(sold_parts_amount_dly + sold_labor_amount_dly) from sales s1  where
(select  SUM(sold_parts_amount_dly + sold_labor_amount_dly) from sales
s2 where s2.location_id = s1.location_id) > 0 group by location_id;


Вложения

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

Предыдущее
От: Ray Stell
Дата:
Сообщение: Re: why not kill -9 postmaster
Следующее
От: Tom Lane
Дата:
Сообщение: Re: why not kill -9 postmaster