All of the responses I've seen to this question so far seem logically wrong, or 
at the very least are different than I would do it, and my proposal is one that 
should work reliably on any DBMS.  You use a subquery in the FROM clause.

select currency, price
from (
   select currency, day, max(time) as time
   from prices
   group by currency, day
) as filter
   inner join prices using (currency, day, time)

The issue here is you want to return other details, the price, associated with 
the latest time per currency-day, and you can't do that in SQL without having a 
select query nested in another one; the inner determines the latest time per 
currency-day and the outer one looks up other info related to it.

The above example should also perform very efficiently, besides being reliably 
correct rather than just accidentally correct.

-- Darren Duncan

Reply via email to