I am storing the response to various rpc calls in a mysql table with the following fields:
Table: rpc_responses
timestamp (date)
method (varchar)
id
Try this...
SELECT o1.id, o1.timestamp, o1.method, o1.response
FROM rpc_responses o1
WHERE o1.timestamp = ( SELECT max(o2.timestamp)
FROM rpc_responses o2
WHERE o1.id = o2.id )
ORDER BY o1.timestamp, o1.method, o1.response
...it even works in Access!
i used this,worked for me
select max(timestamp),method,id from tables where 1 group by method,id order by timestamp desc
Self answered, but I'm not sure that it will be an efficient enough solution as the table grows:
SELECT timestamp,method,id,response FROM rpc_responses
INNER JOIN
(SELECT max(timestamp),method,id FROM rpc_responses GROUP BY method,id) latest
USING (timestamp,method,id);
Use this solution with caution:
it is not guaranteed to work in future versions of mysql
it is not known to work in mariadb 5.5
This can query may perform well, because there are no joins.
SELECT * FROM (
SELECT timestamp, method, id, response
FROM rpc_responses
WHERE 1 # some where clause here
ORDER BY timestamp DESC
) as t1
GROUP BY method
The "group by", collapses the result set on method, and returns only 1 row per method, the most recent one, because of the ORDER BY timestamp DESC in the inner query.
FYI, PostgreSQL has a way of doing this built into the language:
SELECT DISTINCT ON (method) timestamp, method, id, response
FROM rpc_responses
WHERE 1 # some where clause here
ORDER BY method, timestamp DESC
...is more than one year later but i might help someone To select all the queries starting from latest
SELECT *
FROM rpc_responses
ORDER BY timestamp DESC
Subquery is very taxing when the data set becomes larger.
Try this:
SELECT t1.*
FROM rpc_responses AS t1
INNER JOIN rpc_responses AS t2
GROUP BY t1.method, t1.id, t1.timestamp
HAVING t1.timestamp=MAX(t2.timestamp)
ORDER BY t1.timestamp, t1.method, t1.response;