Skip to content
Advertisement

postgres array of increments/decrements

I have a table like:

I want this output:

Basically the array has the count of consecutive increments/decrements. The table has around 10 million rows. Right now doing via a script which makes it very slow. Can it be done through SQL.

I am using Postgres 12.4

Advertisement

Answer

This is a type of gaps-and-islands problem

Here is a db<>fiddle.

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