SQLize Online / PHPize Online

Articles    Popular

How to get user last activity record

Tags: SQL Server MySQL 5.x MySQL 8.x PostgreSQL SQLite

Data: user_activity

user_id activity activity_date
1login2020-01-01 15:16:17
2login2020-01-01 15:18:19
3login2020-01-01 19:24:41
3logout2020-01-01 20:08:01
1logout2020-01-02 03:16:17

Question: What are the possible ways to receive last acitvity for each user from users activities log table?

Solution 1: Using window functions

Tags: SQL Server MySQL 8.x PostgreSQL SQLite

Connect to the instance of SQL Server, and then run the following query:


Use this link for 
  SQL fiddle

Solution 2: Using sub-query

Tags: MySQL 5.x

Connect to the instance of SQL Server, and then run the following query:


  Use this link for 
    MySQL fiddle code test  
    


If you find an error on this page, please contact us via feedback form