select landscape,workschedule_type,datespecified, day_type into #cal from hr_tm_workschedule_calender where datespecified >= '20230901' and day_type = 'OFF' select landscape,emp_id,start_date,attendance_type into #md19 from hr_md_emp_md0019 where start_date >= '20230901' and attendance_type = 'P' select * into #md25 from hr_md_emp_md0025 where start_date >= '20230901' --select * from hr_md_emp_md0025 where emp_id ='20230211' --select * from #cal --select * from #md19 select DISTINCT md19.emp_id, cal.datespecified, cal.workschedule_type, cal.day_type, md19.attendance_type into #tempA from #cal cal inner join #md19 md19 ON cal.datespecified = md19.start_date select DISTINCT md25.emp_id,tempA.datespecified,tempA.workschedule_type,tempA.day_type,tempA.attendance_type into #tempFinal from #md25 md25 left join #tempA tempA on md25.emp_id = tempA.emp_id and md25.ws_type = tempA.workschedule_type where md25.start_date <= tempA.datespecified and end_date >= tempA.datespecified --select * from #tempFinal UPDATE hr_md_emp_md0019 SET attendance_type = 'P1' FROM hr_md_emp_md0019 U19 inner join #tempFinal final on U19.emp_id = final.emp_id and U19.start_date = final.datespecified drop table #tempA drop table #tempFinal drop table #md25 drop table #cal drop table #md19