I think you want:
select t.*,
(case when state = 'DESLIGADO' then id
else max(case when state = 'DESLIGADO' then id end) over (order by id)
end) as desligado_id
from t;
In turn, this can be simplified to:
max(case when state = 'DESLIGADO' then id end) over (order by id)
You could phrase this using lag()
but only if you know that the states are always interleaved.
In standard SQL (and some databases), this could also be expressed as:
max(id) filter (where state = 'DESLIGADO') over (order by id)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…