Skip to content
Advertisement

Find start and end dates when one field changes

I have this data in a table

When ever the value in FIELD_D changes it forms a group and I need the min and max dates in that group. The query shoud return

The examples that I have seen so far have the data in Field_D being unique. Here the data can repeat as shown, First it is “N” then it changes to “P” and then back to “N”.

Any help will be appreciated

Thanks

Advertisement

Answer

You can use analytic functions – LAG, LEAD, and COUNT() OVER to your advantage, if they are supported by your SQL implementation. SQL Fiddle here.

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