tags:

views:

1026

answers:

5

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 I'm getting, but instead of the older record I need the most recent (LAST datagps.Fecha / datagps.Hora).

How can I accomplish this?

+1  A: 

Add ORDER BY datagps.Fecha DESC, datagps.Hora DESC LIMIT 1 to your query.

Lex
Thanks for your help!... but no, what this does is that order the result I was getting, but is not bringing the most recent record. Another words, this query brings back a list of 5 vehicles, your suggestion order that list by date and time ok, but the date and time are the first records of those vehicles (eg.: 2007-07-26) and I need the most recent. I think the solution is in the WHERE, or using max(datagps.Fecha) or something like that, not with "order by" ... sugestions?I need to pull the latest recorded records
I don't think I understand what you mean. Can you give an example?
Lex
A: 

Here is a screenshot of the query results, wich are ok, except for the fact that brings the first (oldest) records, instead of the newest:

Check this image: http://img7.imageshack.us/i/samplecx.jpg/

As you can see I have records from 2006, what I need are today's position, current date and time.

Thanks again!

So these are the results of the query you posted in the original question? What happens if you add the ORDER BY and LIMIT clauses I suggested in my first answer?
Lex
If I add the ORDER BY and LIMIT clauses what it does is show only one record, but still a first record, not the most recent. If I add only the ORDER BY clause then shows this same results but sorted bye date.
Yeah, but by using ORDER BY on the date and time columns you ensure that you will get the most recent. Unless the measurement of what is the most recent is not based on those columns.
Lex
I guess I have you guys very confused... sorry! Let me see if I can ilustrate the situation better, check this image:http://img7.imageshack.us/img7/8985/screenshotsql.jpgLex: The measurement of what is the most recent it is based on those columns.I think the solution has something to do with max(Fecha)... but i dont know how to write such select... think about it...I really appreciate your help!
Ah, now I see what you mean. Maybe you can omit the GROUP BY, so you will get all the results and then use LIMIT 5 to get only five records?
Lex
A: 

I'm not sure why you are having any problems with this as Lex's answers seem good.

I would start putting ORDER BY's in your query so it puts them in an order, when it's showing the record you want as the first one in the list, then add the LIMIT.

If you want the most recent, then the following should be good enough:

ORDER BY datagps.Fecha DESC, datagps.Hora DESC

If you simply want the record that was added to the database most recently (irregardless of the date/time fields), then you could (assuming you have an auto-incremental primary key in the datagps table (I assume it's called dataID for this example)):

ORDER BY datagps.dataID DESC

If these aren't showing the data you want - then there is something missing from your example (maybe data-types aren't DATETIME fields? - if not - then maybe a CONVERT to change them from their current type before ORDERing BY would be a good idea)

EDIT: I've seen the screenshot and I'm confused as to what the issue is still. That appears to be showing everything in order. Are you implying that there are many more than 5 records? How many are you expecting?

Amadiere
Let me explain better: Im expecting 5 records, but, for example: The first record, Ficha "SKODA" the date is 2009-09-15, that is the date of the first vehicle report, what I need is the report from, let's say, today, 2009-09-26. Another words, it shows the results from the first record, and I need the last record.
A: 

Do you mean: for each record returned, you want the one row from the table datagps with the latest date and time attached to the result? If so, how about this:

# To show how the query will be executed
# comment to return actual results
EXPLAIN

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)

########### Add this section
LEFT JOIN datagps b ON (
    configgps.Fichagps = b.Fichagps

    # wrong condition
    #AND datagps.Hora < b.Hora
    #AND datagps.Fecha < b.Fecha)

    # might prevent indexes to be used
    AND (datagps.Fecha < b.Fecha OR (datagps.Fecha = b.Fecha AND datagps.Hora < b.Hora))

WHERE b.Fichagps IS NULL
########### 

Group by configgps.Fichagps;

Similar question here only that that one uses outer joins.

Edit (again): The conditions are wrong so corrected it. Can you show us the output of the above EXPLAIN query so we can pinpoint where the bottle neck is?

As hurikhan77 said, it will be better if you could convert both of the the columns into a single datetime field - though I'm guessing this would not be possible for your case (since your database is already being used?)

Though if you can convert it, the condition (on the join) would become:

    AND datagps.FechaHora < b.FechaHora

After that, add an index for datagps.FechaHora and the query would be fast(er).

Vin-G
Hi all!Vin-G, something wrong here, I had to stop the process after more than 10 - 15 minutes with the processor all the way at 100%
Might be that no indexes are being used by the query, and you are using very large tables? Are there indexes for datagps.Hora and datagps.Fecha? Can you do an explain on the select? Also try rexem's answer below which uses inner queries.
Vin-G
Oh wait, i think there's something wrong with the conditions, let me correct it.
Vin-G
Corrected it, can you do the explain and show us the output?
Vin-G
A: 

What you probably want is getting the maximum of (Fecha,Hora) per grouped dataset? This is a little complicated to accomplish with your column types. You should combine Fecha and Hora into one column of type DATETIME. Then it's easy to just SELECT MAX(FechaHora) ... GROUP BY Fichagps.

It could have helped if you posted your table structure to understand the problem.

hurikhan77