2

I need to return the last row of each device in my table location

My table name is: Location

+------------+----------+--------------+--------------+----------+  
| locationId | deviceId | dataRegistro | horaRegistro | location |  
+------------+----------+--------------+--------------+----------+  
|         50 |        1 |   2012-11-07 |     15:35:00 |      A12 |  
+------------+----------+--------------+--------------+----------+  
|         51 |        1 |   2012-11-07 |     15:37:40 |       B2 |  
+------------+----------+--------------+--------------+----------+  
|         52 |        2 |   2012-11-07 |     15:35:12 |       B8 |  
+------------+----------+--------------+--------------+----------+  
|         53 |        2 |   2012-11-07 |     15:35:40 |      50C |  
+------------+----------+--------------+--------------+----------+  
|         54 |        2 |   2012-11-07 |     15:40:00 |      94A |  
+------------+----------+--------------+--------------+----------+  

From select last row from a one device I do

select L from Location L where deviceId = :deviceId order by "dataRegistro" DESC, "horaRegistro" DESC limit 1

Now how do I select all the latest location of every device? :( I use Java JPA.

thanks,

Evandro

7 Answers 7

5

If the IDs are ordered, so a record recorded later corresponds to a later date, you can use this query (I wouldn't bother converting the strings to dates, that would make it slower than acceptable.):

select L from Location L where deviceId = :deviceId order by L.locationId DESC 

and restrict the amount of rows to be returned in the Java code:

//EntityManager em; defined earlier
Query q = em.createQuery("select L from Location L where deviceId = :deviceId order by L.locationId DESC ");
q.setParameter("deviceId", "theDeviceIdIWantToQuery");
q.setMaxResults(1);
List<Location> results=query.getResultList();

This will be an empty list, if there are no rows for the given deviceId, or a List containing 1 Location item - the last.

If you want to select the latest locations for all devices in one go, you can use an IN clause and a subquery:

select L2 FROM Location L2 WHERE L2.locationId IN
    (select L.locationId from Location L 
    where L.deviceId = :deviceId 
    order by L.locationId DESC 
    group by L.deviceId)

Sadly, JPQL doesn't permit using subqueries in the FROM clause, so while that would be my choice of doing this, it is only possible by using the native query function, which is a lot more inconvenient to use.

By the way, in my opinion storing timestamps like you do is a bad practice. It is not properly indexable, gives troubles when querying and ordering, just a plain nightmare. I'd seriously rethink this part of your schema.

1

yes I use JPA, this example is just to demonstrate what I need

My JPA query to select one device is

Query q = em.createQuery("select TL from TRKLocation TL where   
TL.device.deviceId = :deviceId and TL.msgError = '' order by 
TL.dataRegistro DESC, TL.horaRegistro DESC");
        q.setParameter("deviceId", deviceId);           
        q.setMaxResults(1);

list = q.getResultList();

Now, i need query to select last location from all devices :)

0

How about something like this:

SELECT locationId
  FROM location
 WHERE dateregistro + horaregistro = (SELECT MAX(datergistro + horaregistro)
                                        FROM location
                                       WHERE deviceid = :deviceid)
   AND deviceid = :deviceid

I'm not sure about the correctness of the date calculation, but maybe you can create a new column, where date and time are combined.

0
0

try this

select top (1) location from location where deviceid=:deviceid 
 order by dateregistro,horaregistro desc
3
  • Hi, thanks for replying, but I want to select the last location of all devices
    – user690093
    Nov 8, 2012 at 13:20
  • @user690093 this gives you the last location of the device of which you give the deviceID
    – SRIRAM
    Nov 8, 2012 at 13:22
  • 2
    This is not JPQL... (there is no TOP 1 in that...)
    – ppeterka
    Nov 8, 2012 at 13:24
0

I do not understand why you don't store a simple timestamp instead of having two columns for date and time. This makes it way more difficult to run the select you want. This is an example how it should work if a timestamp where available (From a look into the JPQL Spec section 4.6.16 this should work)

Select loc From Location loc where loc.ts_registered = 
(Select MAX(subloc.ts_registered) From Location subloc where subloc.deviceId = loc.deviceId)

I did not test it but I think this should work. But be aware that this query will run quite slowly on medium to large data sets. You should definitely add an index to column deviceId.

Besides I would recommend you to rethink your data model since this query requires building of cartesian product (which will slow down rather quickly with growing data)

0

In case the dateregistro and horaregistro are strings you could try this query:

select * from location l1
where dateregistro + horaregistro = (select max(dateregistro + horaregistro) from
location l2 where l2.deviceid = l1.deviceid)

Better, if the locationid field is a unique and sorted key you could apply the where condition on it:

select * from location l1
where locationid = (select max(locationid) from
location l2 where l2.deviceid = l1.deviceid)
0
Query q= em.createQuery("Select D from DeviceInfo D WHERE D.devLastPing IN (SELECT MAX(DD.devLastPing) FROM DeviceInfo DD WHERE DD.devLastPing >= :startDate AND DD.devLastPing <= :endDate)");

        q.setParameter("startDate", startDate)
                .setParameter("endDate", endDate);
        List<DeviceInfo> list = (List<DeviceInfo>)q.getResultList();

You can use something like that. Sorry in my case I was using it for device purpose. And sorry, I know it is too late but It can help someone, and again sorry if someone previously replied. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.