I am looking for a SQL query or a series of SQL queries.
Schema
- I have a logging table with three columns:
id
,event_type
, andtimestamp
- The IDs are arbitrary text, generated randomly at runtime and unknown to me
- The event types are numbers from a finite collection of known event types
- The timestamps are your typical int64 epoch timestamp
- A single ID value may have 1 or more rows, each with some value for
event_type
. representing a flow of events associated with the same ID - For each ID, its collection of rows can be sorted by increasing timestamp
- Most times, there will be only one occurrence of an ID + event type combination, but rarely, there could be two; not sure this matters
Goal
What I want to do is to query the number of distinct combinations of event types (sorted by timestamp). For example, provided this table:
id event_type timestamp
-----------------------------------------
foo event_1 101
foo event_2 102
bar event_2 102
bar event_1 101
foo event_3 103
bar event_3 103
blah event_1 101
bleh event_2 102
backwards event_1 103
backwards event_2 102
backwards event_3 101
Then I should get the following result:
combination count
-------------------------------
[event_1,event_2,event_3] 2 // foo and bar
[event_3,event_2,event_1] 1 // backwards
[event_1] 1 // blah
[event_2] 1 // bleh