Skip to content
Advertisement

Complicated group by – windows

Can you please help with the following grouping: Name can have states that can be repeated several times and it is essential in output to have all the state changes over time and to have the starting date of the state

Here is the input data:

Desired output

Advertisement

Answer

Assuming you want to find the start date per consecutive sequence of state per name, this is one solution:

Test case

Result:

The basic form works with most databases, PG, MySQL 8.0, MariaDB 10.2.2+, SQL Server, Oracle (with some adjustments in “date” quoting, type (VARCHAR2) and INSERT form).

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