I have three columns, user_id, event_name and event_time.
A user performs multiple events and the time for which is stored.
From the events, I want to get the event just before the LATEST 'failure' event (I am using BigQuery).
I have tried this:
WITH numberedlogtable AS
(
SELECT
User_ID, Event_Name,
ROW_NUMBER() OVER (PARTITION BY User_ID ORDER BY Event_Time DESC) AS RN
FROM
tablename
WHERE
date(Event_Time) BETWEEN '2024-02-12' AND '2024-02-24'
)
SELECT DISTINCT User_ID, Event_Name
FROM numberedlogtable
WHERE RN IN (SELECT RN+i
FROM numberedlogtable
CROSS JOIN (SELECT -1 AS i
UNION ALL
SELECT 0
UNION ALL
SELECT 1) n
WHERE Event_Name = 'fail_screen')
I am getting multiple rows for each user, and as I'm learning SQL, I'm not sure how to solve this issue.