select * from #Issuer_rating
ratg_dt ratg_clss_num
1994-04-22 00:00:00.000 1999
1994-04-28 00:00:00.000 260
1995-07-27 00:00:00.000 270
2001-03-08 00:00:00.000 270
2002-05-15 00:00:00.000 1999
——————————————–
select
ratg_dt
,ratg_clss_num
,next_clss_num = (
select
distinct ratg_clss_num
from #Issuer_rating r
where ratg_dt>a.ratg_dt
and not exists(
select
distinct 1
from #Issuer_rating
where ratg_dt>a.ratg_dt
and ratg_dt<r.ratg_dt
)
)
from #Issuer_rating a order by ratg_dt asc
1994-04-22 00:00:00.000 1999 260
1994-04-28 00:00:00.000 260 270
1995-07-27 00:00:00.000 270 270
2001-03-08 00:00:00.000 270 1999
2002-05-15 00:00:00.000 1999 NULL