Skip to content
Advertisement

SQL how to find continuous count of rows that exceed a value over time

I need to figure out how to query this table such that I find out the continuous count for each ID in which the value is equal or above 1 and the timestamp1 is bigger than timestamp 2. So for example:

ID 1 will have a count of 2 because the 3rd row is 0 and even though the 4th is of value 2 I need for it to be continuous.

ID 2 will have a count of 1 because row 4 timestamp 1 is below timstamp 2.

Please help, have tried a bunch of things

Advertisement

Answer

If I understand correctly, this is a gaps-and-islands problem. You can get each range as:

In Postgres, you can get the maximum using distinct on. I’m guessing that you are using Redshift and under the mistaken impression that it is equivalent to Postgres (it might have been decades ago). You can get what you want using an additional level of aggregation:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement