Re: [professionel] Re: division by zero error in a request - Mailing list pgsql-general

From Oisin Glynn
Subject Re: [professionel] Re: division by zero error in a request
Date
Msg-id 4538D936.2090309@oisinglynn.com
Whole thread Raw
In response to division by zero error in a request  (Bernard Grosperrin <bernard@bgsoftfactory.com>)
List 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;


Attachment

pgsql-general by date:

Previous
From: Ray Stell
Date:
Subject: Re: why not kill -9 postmaster
Next
From: Tom Lane
Date:
Subject: Re: why not kill -9 postmaster