I have a question that a little bit similar with question#66044663 but more complicated.
Here's my dummy data.
I want to get 3 adjacent actions(no duplicate) 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)?
I know the function LAG could be a solution but I have no idea how to avoid the duplicate actions.
Hope someone can light me up. Thanks a million!
Here's the code for generating the dataset.
WITH
src_table AS (
SELECT 'Jack' AS User, 1 AS Sequence, 'Eat' AS Action, '' AS Flag UNION ALL
SELECT 'Jack' AS User, 2 AS Sequence, 'Work' AS Action, '' AS Flag UNION ALL
SELECT 'Jack' AS User, 3 AS Sequence, 'Sleep' AS Action, 'Flag A' AS Flag UNION ALL
SELECT 'Jack' AS User, 4 AS Sequence, 'Exercise' AS Action, 'Flag B' AS Flag UNION ALL
SELECT 'Kenny' AS User, 1 AS Sequence, 'Run' AS Action, '' AS Flag UNION ALL
SELECT 'Kenny' AS User, 2 AS Sequence, 'Eat' AS Action, '' AS Flag UNION ALL
SELECT 'Kenny' AS User, 3 AS Sequence, 'Eat' AS Action, '' AS Flag UNION ALL
SELECT 'Kenny' AS User, 4 AS Sequence, 'Work' AS Action, 'Flag C' AS Flag UNION ALL
SELECT 'Kenny' AS User, 5 AS Sequence, 'Work' AS Action, 'Flag D' AS Flag UNION ALL
SELECT 'May' AS User, 1 AS Sequence, 'Work' AS Action, 'Flag A' AS Flag
)
question from:
https://stackoverflow.com/questions/66060358/sql-to-get-3-adjacent-actions-without-duplicate-from-the-flags 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…