got this table: initial table
Need to get table like this: Target table
what I used:
Select ID_CLIENT, BALANCE, HIST_DT as Start_dt, isnull(lead(hist_dt) over (partition by id_client order by hist_dt asc), '2999.12.31') as End_dt
from (
select ID_CLIENT, ID_STATUS, balance, hist_dt, lag(id_status) over (partition by id_client order by id_status) as Prev_ID_status
from Client_History) a
where a.ID_STATUS = a.Prev_ID_status or a.ID_STATUS = 1
order by ID_CLIENT, HIST_DT
I think its very complicated. Will be glad to hear any suggestions of simplyfing this query.