Hi @ahmed salah ,
Please also check:
select lifecycleid, ZPartID,ZLC,ProccessingDate,(Row_Number+1) as Row_Number
into #tempFullRows
from
(select m.lifecycleid, m.ZPartID,m.ZLC,m.ProccessingDate,0 as Row_Number
from parts.LifeCycleMaster m inner join #TempLC t on m.ZPartID=t.ZPartID
and t.status is null
union all
select * from #arrangeHistory)as tempdata
Or:
select lifecycleid, ZPartID,ZLC,ProccessingDate,
ROW_NUMBER() OVER(Partition by ZPartID ORDER BY ProccessingDate DESC) AS Row_Number
into #tempFullRows
from
(select m.lifecycleid, m.ZPartID,m.ZLC,m.ProccessingDate,0 as Row_Number
from parts.LifeCycleMaster m inner join #TempLC t on m.ZPartID=t.ZPartID
and t.status is null
union all
select * from #arrangeHistory)as tempdata
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".