Skip to content
Advertisement

Find groups containing 6 consecutive 1s in one column

I have a table with 2 columns:

  • val with values: 0 or 1
  • id with unique identifiers

How do I to find id with 6 values = 1 in a row.

In the example above: id = 9, id = 10.

It is desirable not to use loops (cursors or while), but something like sum(...) over(...).

Advertisement

Answer

You can use running sum over a window frame that contains exactly 6 rows (5 prior plus current row):

Adjust the size of the window and where clause to match the desired value.

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