I have a table like this:
id | action | user_id
1 'test' 'person1'
2 'entered' 'person2'
3 'KEYPHRASE1' 'person1'
4 'entered' 'person1'
I need to do a select and return the user_id of the ‘entered’ action. However, I need to grab this ‘entered’ action only after the last occurence of either of these phrases: ‘KEYPHRASE1’, ‘KEYPHRASE2’,
‘KEYPHRASE3’, ‘KEYPHRASE4’, ‘KEYPHRASE5’, and ‘KEYPHRASE6’. If the ‘entered’ action does not occur after the last occurence of either of these KEYPHRASEs, then return null.
(So, in this example, the query should return ‘person1’ and not ‘person2’)
This seems like a correlated subquery in the
whereclause solution:The above checks that the actions are after one of the last ones. Answering the question as written (after the last one) can be done with an aggregation: