Subject | Re: Help Group by |
---|---|
Author | Svein Erling Tysvær |
Post date | 2006-03-16T12:50:35Z |
This sounds like a case where NOT EXISTS is at its best:
SELECT O.ORDER_ID, O.COD_ART, O.COST
FROM ORDERS O
WHERE NOT EXISTS(SELECT * FROM ORDERS O2
WHERE O2.COD_ART = O.COD_ART
AND (O2.COST < O.COST
OR (O2.COST = O.COST AND O2.ORDER_ID < O.ORDER_ID))
This is assuming you only want to return the first ORDER_ID if there
are several with the same COST for a COD_ART. If you want all of them
returned, simply skip the OR and all after that.
This helps,
Set
SELECT O.ORDER_ID, O.COD_ART, O.COST
FROM ORDERS O
WHERE NOT EXISTS(SELECT * FROM ORDERS O2
WHERE O2.COD_ART = O.COD_ART
AND (O2.COST < O.COST
OR (O2.COST = O.COST AND O2.ORDER_ID < O.ORDER_ID))
This is assuming you only want to return the first ORDER_ID if there
are several with the same COST for a COD_ART. If you want all of them
returned, simply skip the OR and all after that.
This helps,
Set
--- In firebird-support@yahoogroups.com, "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