Skip to content
Advertisement

Oracle – Find the group of most consecutive occurrence of a value in a column

I have a table structure called test_table like this:

and so on..

It is a large table and has about 3000 rows. I want to find the section of the table (start and end timestamp) for the longest running occurrence of “C” values. For example in the above case it would be 8:46:59 to 8:48:55. Is there an oracle query to extract that longest set of data from the large table?

Advertisement

Answer

There is a trick using the difference of row_number() values that you can use to identify sequences:

To get just one row, you can use fetch first 1 row only in Oracle 12c+. In earlier versions, you can use:

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