1

If i have the below data:

    route_name     updatedDate          route_id
    FF          15/06/2012 0:00         22
    DD          15/06/2012 0:00         31
    FF          16/06/2012 0:00         23
    FF          17/06/2012 0:00         24
    DD          17/06/2012 0:00         32
    DD          17/06/2012 18:00            
    FF          18/06/2012 0:00         
    FF          18/06/2012 19:00        40
    DD          20/06/2012 0:00         60

I want to run a query where i only see the route_id based in the latest updatedDate for the given route_name, so for example for route_name FF above the route_id should be 40.

I tried to do this using:

    select route_name,route_id,max(updatedDate) from route_de 
    group by route_name,route_id,updatedDate
    having count(*) >= 1
    order by route_name

but i get many repeated rows because of the updatedDate field (this is a big table). Can someone please suggest a way to archive the above?

4

1 回答 1

2
select route_de.route_name, route_de.route_id from route_de join
(select max(updatedDate) as maxDate, route_name from route_de group by route_name) maxRoutes
on maxRoutes.maxDate = route_de.updatedDate and maxRoutes.route_name = route_de.route_name
于 2012-06-20T04:57:58.117 回答