Skip to content
Advertisement

SQL query to find user by it’s specific latest action

I have following db scheme below where all user actions are collected:

The goal is to find all recently blocked users. So the expected result is to find 1 and 3 user ids since those users were recently blocked.

I’ve tried to play with following SQL below, but still do not have good understending how to finalize this:

Currently query is able to return only following:

For the result above I need to all users where action_id = 1 and timestamp is bigger than in action_id = 2

Advertisement

Answer

One solution is to use ROW_NUMBER inside an embedded query and then filter the result for the last timestamp and the desired action_id.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement