I have data that looks as follows.
timestamp | productid | measure |
---|---|---|
2022-01-01 00:00:00 | 1 | 4 |
2022-01-01 00:01:00 | 1 | 5 |
2022-01-01 00:02:00 | 1 | 6 |
2022-01-01 00:01:00 | 2 | 5 |
2022-01-01 00:00:00 | 2 | 7 |
2022-01-01 00:01:00 | 3 | 9 |
The table records the telemetry of the product at 1-minute intervals.
The table has an incremental id which is not in any way uniquely identifiable. The primary index is a combination of productid and timestamp.
I want a query that can select productid and timestamp of the 1st row where the measure is between 4 and 5 for 10 continuous timestamps when the data is partitioned by productid and ordered by timestamp.
I tried creating a list of all measures that satisfy the condition and then seeing if they are continuous, but it is taking really long.
2
Answers
Not entirely sure, but does this give you what you need?
Another gaps and islands problem. You say "over 10" so I will treat a run of any length as one unit:
The problem doesn’t fundamentally change whether you need to partition by product or not. Just comment out those sections if not. The trick is to mark the spots where there’s a transition and based on that the subsequent run of rows can be associated with a common and unique number (
grp
).