Here is my data :
app_id event_type event_time
1 event1 2020-22-03 04:05:03
1 event3 2020-22-03 04:05:04
1 event2 2020-22-03 04:05:05
1 event3 2020-22-03 04:05:10
1 event1 2020-22-03 04:05:11
1 event2 2020-22-03 04:05:12
2 event3 2020-22-03 04:05:04
I want to count the sum of two events for each app_id
:
- Number of
event3
- Number of
event1
followed immediately (based onevent_time
) byevent2
With the data above, the output would be :
app_id count
1 3 <--- (2 * event3 + event1 followed by event2)
2 1
The number of event3
can be calculated with :
SELECT app_id,
count(event_type = 'event3')
FROM test_table
GROUP BY app_id;
For the second count, I guess the data need to be GROUP BY app_id
combined with ORDERBY event_time
I also saw other answers for checking consecutive values (Count Number of Consecutive Occurrence of values in Table), but I couldn't adapt it to my use case