Skip to content
Advertisement

SQL query grouping by range

Hi have a table A with the following data:

I’d like to have the following result:

My DB is DB2/400. I have tried with ROW_NUMBER partitioning, subqueries but I can’t figure out how to solve this.

Advertisement

Answer

I understand this as a gaps-and-island problem, where you want to group together adjacent rows that have the same PA.

Here is an approach using the difference between row numbers to build the groups:

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