Skip to content
Advertisement

Find date range for every row. LEAD/LAG but with different types/actions

I have a table with following data:

There are two actions possible. ‘Remove’ and ‘Install’. How can I find Place for Code A for given date? For example for 2015 it will be Place 1. For 2020 it will be Place 2. For 2025 it will be Place 3.

I think the first and the most difficult step is to transform it to table that contains start and end date. Then I can use between function.

Example output:

I have tried with LEAD/LAG but we have two different actions here so I think it is not the best idea. How could I deal with this problem?

Thanks.

Advertisement

Answer

I understand. The following uses union all to be sure it gets all values.

The first subquery handles the case where the data starts with 'Remove'. The second takes all 'installs' and sets up the date to the next record.

Note that this will work even if the places are interleaved in the data (so a code returns to a place after some period of time).

Here is a db<>fiddle.

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