From the following db,
id userid eventid created_at
1 1 100 2010-01-01
2 1 101 2010-02-01
3 1 100 2010-03-01
4 1 101 2010-04-01
5 2 100 2010-05-01
6 2 100 2010-06-01
I'm trying to get the latest activity of of each user based on eventid, with an output like this:
6 2 100 2010-06-01
4 1 101 2010-04-01
3 1 100 2010-03-01
Use group by
and aggregation
select userid,envid,max(id) as id, max(created_at) as created_at
from tablename a
group by userid,envid
Use Rownumber
SELECT *
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY userid, eventid ORDER BY id DESC) as RId,
t.*
FROM Tablename t
) T
WHERE RId = 1 ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.