【发布时间】:2019-12-28 16:46:25
【问题描述】:
得到这张表: initial table
需要得到这样的表: Target table
我用了什么:
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
我认为它非常复杂。很高兴听到任何简化此查询的建议。
【问题讨论】:
-
我认为您可以在
Balance更改为id_client时进行查询
标签: sql date simplify period balance