I'm trying to correlate two types of events for users. I want to see all event "B"s along with the most recent event "A" for that user prior to the "A" event. How would one accomplish this? In particular, I'm trying to do this in Postgres.
I was hoping it was possible to use a "where" clause in a window function, in which case I could essentially do a LAG() with a "where event='A'", but that doesn't seem to be possible.
Any recommendations?
Data example:
|user |time|event|
|-----|----|-----|
|Alice|1 |A |
|Bob |2 |A |
|Alice|3 |A |
|Alice|4 |B |
|Bob |5 |B |
|Alice|6 |B |
Desired result:
|user |event_b_time|last_event_a_time|
|-----|------------|-----------------|
|Alice|4 |3 |
|Bob |5 |2 |
|Alice|6 |3 |
Here is one method:
It is possible that the correlated subquery/lateral join would have better performance.
Just tried Gordon's approach using PostgreSQL 9.5.4, and it complained that
which means using
lag()
withFILTER
is not allowed. So I modified Gordon's query usingmax()
, a different window frame, and CTE:Verified that this works with PostgreSQL 9.5.4.
Thanks to Gordon for the
FILTER
trick!There is not need for window functions here. Just find all
B
events, and for each one of them, find the most recentA
of the same user via a subquery. Something like that should do it:I assume that the table is called
t
(I used it twice).