If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQL select to return most recent record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-26-09, 07:09
SenTnel SenTnel is offline
Registered User
 
Join Date: Sep 2009
Posts: 1
MySQL select to return most recent record

Hi!

We have a database that stores vehicle's gps position, date, time, vehicle identification, lat, long, speed, etc., every minute.

The following select pulls each vehicle position and info, but the problem is that returns the first record, and I need the last record (current position), based on date (datagps.Fecha) and time (datagps.Hora). This is the select:

SELECT configgps.Fichagps, datacar.Ficha, groups.Nombre, datagps.Hora, datagps.Fecha, datagps.Velocidad, datagps.Status, datagps.Calleune, datagps.Calletowo, datagps.Temp, datagps.Longitud, datagps.Latitud, datagps.Evento, datagps.Direccion, datagps.Provincia FROM asigvehiculos INNER JOIN datacar ON (asigvehiculos.Iddatacar = datacar.Id) INNER JOIN configgps ON (datacar.Configgps = configgps.Id) INNER JOIN clientdata ON (asigvehiculos.Idgroup = clientdata.group) INNER JOIN groups ON (clientdata.group = groups.Id) INNER JOIN datagps ON (configgps.Fichagps = datagps.Fichagps) Group by Fichagps;

I need same result Im getting, but instead of the older record I need the most recent
(LAST datagps.Fecha / datagps.Hora).


How can I accomplish this?

Thanks a lot!
Reply With Quote
  #2 (permalink)  
Old 09-26-09, 11:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by SenTnel
I need same result Im getting, but instead of the older record I need the most recent
(LAST datagps.Fecha / datagps.Hora).
in your query, you have GROUP BY -- is this an attempt to get only one datagps row?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-26-09, 23:42
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
Looks like what you want is pretty much selecting the max rows per each group. I've came up with similar issues and have posted a blog post for it here: Microshell Optimizing SQL that selects the max/min/etc from a group.

Hope it helps.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On