select *,dateadd(month,12,LJ_DATE)
from tb as t
where not exists(select * from tb where CPCODE=t.CPCODE and LJ_DATE>t.LJ_DATE)
from tb as t
where not exists(select * from tb where CPCODE=t.CPCODE and LJ_DATE>t.LJ_DATE)
from tb
group by cpcode, sb_ym
select *,dateadd(month,12,LJ_DATE)
from tb as t
where not exists(select * from tb where CPCODE=t.CPCODE and LJ_DATE<t.LJ_DATE)
t.*,dateadd(mm,12,t.LJ_DATE)
from
tabname t
where
not exists(select 1 from tabname where CPCODE=t.CPCODE and SB_YM=t.SB_YM and LJ_DATE<t.LJ_DATE)