SQL to get 2 adjacent actions from the flag
问题 hope you are doing well! I have an dummy data as below. I want to get 2 adjacent actions from the flag by each user. Here's the chart to describe my thought. Here's what I want: How can I implement SQL(I use Google Bigquery)? Hope someone can light me up. Thanks a million! 回答1: You seem to want lag() . I would leave the "action sequence" as two separate columns: select user, prev_action, action, flag from (select t.*, lag(action) over (partition by user order by sequence) as prev_action from