Get most recent row for given ID

Use the aggregate MAX(signin) grouped by id. This will list the most recent signin for each id. SELECT id, MAX(signin) AS most_recent_signin FROM tbl GROUP BY id To get the whole single record, perform an INNER JOIN against a subquery which returns only the MAX(signin) per id. SELECT tbl.id, signin, signout FROM tbl INNER JOIN … Read more

How can I select rows with most recent timestamp for each key value?

For the sake of completeness, here’s another possible solution: SELECT sensorID,timestamp,sensorField1,sensorField2 FROM sensorTable s1 WHERE timestamp = (SELECT MAX(timestamp) FROM sensorTable s2 WHERE s1.sensorID = s2.sensorID) ORDER BY sensorID, timestamp; Pretty self-explaining I think, but here’s more info if you wish, as well as other examples. It’s from the MySQL manual, but above query works … Read more

How to select the rows with maximum values in each group with dplyr? [duplicate]

Try this: result <- df %>% group_by(A, B) %>% filter(value == max(value)) %>% arrange(A,B,C) Seems to work: identical( as.data.frame(result), ddply(df, .(A, B), function(x) x[which.max(x$value),]) ) #[1] TRUE As pointed out in the comments, slice may be preferred here as per @RoyalITS’ answer below if you strictly only want 1 row per group. This answer will … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)