SQL window function with a where clause?

前端 未结 2 1891
别跟我提以往
别跟我提以往 2021-02-12 17:50

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

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-12 18:34

    There is not need for window functions here. Just find all B events, and for each one of them, find the most recent A of the same user via a subquery. Something like that should do it:

    SELECT
        "user",
        time AS event_b_time,
        (SELECT time AS last_event_a_time
         FROM t t1
         WHERE "user"=t.user AND event='A' AND time

    I assume that the table is called t (I used it twice).

提交回复
热议问题