Subject Re: [firebird-support] Re: Help Group by
Author Adomas Urbanavicius
Graeme Edwards wrote:

>Something really horrible like this might work but it would be atrociously
>slow and a stored procedure would have to be
>
>vastly superior.
>
>
This is not horrible, this is use of features of FB.
Because subselect uses argument of grouped values, it will be used as
little times as there are groups. So, there will be same reads, as with
your SP.
Adomas



>
>
>The first subselect is designed to return the min value of cost, and the
>second one the first order_ID which matches this minimum value.
>
>
>
>The distinct is necessary otherwise one record for each order rather than
>for each cod_art value will be returned.
>
>
>
>Select Distinct Orders1.Cod_Art,
>
>(Select Min(Orders2.Cost) from Orders Orders2 where
>Orders1.Cod_Art=Orders2.Cod_Art),
>
>(Select first 1 Orders3.Order_ID from Orders Orders3 where
>Order3.Cod_Art=Orders1.Cod_Art and Orders3.Cost=(Select min(Orders4.Cost)
>from Orders Orders4 where Orders4.Cod_Art=Orders3.Cod_art) order by
>Orders3.Order_ID ASC )
>
>>From Orders Orders1
>
>
>
>
>
>Syntax not checked, but idea :
> select
> orders1.cod_art,
> min(MinCost),
> ( select first 1 orders2.order_id from orders orders2
> where
> orders2.cod_art = orders1.cod_art order by mincost asc
> ) ///subselect returns first 1 order_id with cod_art =
>order1.cod_art and min(mincost)
> from
> orders orders1
> group by orders1.cod_art;
>
>
>Adomas
>
>
>
>Alberto Pesce wrote:
>
>
>
>>Hi all collaborators,
>> Based on the example I need from table orders
>>
>>order_ID Cod_art Cost
>>-------- ------- ----
>>1 1 10
>>2 2 20
>>3 1 15
>>4 3 20
>>5 3 22
>>
>>this from the query
>>
>>Cod_art MinCost order_ID
>>------- -------- ---------
>>1 10 1
>>2 20 2
>>3 20 4
>>
>>Thank very much
>> Bye
>>
>>
>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>
>>Visit http://firebird.sourceforge.net and click the Resources item
>>on the main (top) menu. Try Knowledgebase and FAQ links !
>>
>>Also search the knowledgebases at http://www.ibphoenix.com
>>
>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>
>>Yahoo! Groups Links
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
>
>
>


--

Pagarbiai / Best Regards,
Adomas Urbanavicius
+37068543336