Home > Software design >  How to find out first occurrence of each phase
How to find out first occurrence of each phase

Time:10-22

I am working on customer data where customer phase can change based on different factors. I need to find out every phase and the time it was changed to that phase.

Sample Data:

enter image description here

Expected Result:

enter image description here

I tried to use Lag function but it is repeating.

Thank you

CodePudding user response:

lag can help here, but you need to add some filtering and deal with null in lag result

with ordered_data as (
    select *
         , lag(phase) over (order by phase_date desc) prev_phase
         , lag(phase_date) over (order by phase_date desc) prev_phase_date
    from monitoring.st1
)
select *
from ordered_data
where phase != prev_phase

union

(
    select *
         , phase as prev_phase
         , phase_date as prev_phase_date
    from monitoring.st1
    order by phase_date
    limit 1
)
order by phase_date desc

it generates the following result, where prev_phase and prev_phase_date hold result which you're looking for

customerid phase phase_date prev_phase prev_phase_date
A Hold 2021-07-08 Active 2021-08-08
A Suspended 2020-11-10 Hold 2021-01-01
A Active 2020-10-01 Suspended 2020-11-10
A Hold 2020-08-08 Active 2020-10-01
A Active 2020-03-06 Hold 2020-05-05
A Active 2020-01-01 Active 2020-01-01
  • Related