Subject RE: [firebird-support] Re: SQL - Most recent order for each product
Author Alan McDonald
> SELECT
> DISTINCT
> P.PRODUCT_ID,
> O.ORDER_DATE,
> ORDER_ITEM.PRICE
>
> FROM PRODUCTS P
>
> JOIN ORDER_ITEMS I
> ON P.PRODUCT_ID = I.PRODUCT_ID
>
> JOIN ORDERS O
> ON I.ORDER_ID = O.ORDER_ID
>
> JOIN CUSTOMER C
> ON C.CUSTOMER_ID = O.CUSTOMER_ID
>
> WHERE
> C.CUSTOMER_ID = 10300
>
> GROUP BY P.PRODUCT_ID, O.ORDER_DATE, I.PRICE
> HAVING ORDER_DATE = MAX(ORDER_DATE)
> ORDER BY 1,2

correct me if I'm wrong but the group by clause will order by - by default.
there's no need for the order by clause when you are grouping by
Alan