Update Column on Condition before and after.

Analyst_SQL 3,576 Reputation points
2023-07-05T06:57:44.7933333+00:00

I want ,when employee Duty is marked with '0' before 'Sun' means ('Sat') and after 'Sun' means ('Mon') in table #tbl_Emp_Register.Then update query required,which will check and marked that day with '2'.as i attached image for expected output.

Create table #EmpMaster (EnrollNumber int, empName varchar(50))  
Create table #MonthDate (Day varchar(50),Date date,Holiday varchar(50))  


  
 Create table #tbl_Emp_Register  ( EnrollNumbeR int,Date date,Day varchar(5),Duty int)  
  
                     Insert into #EmpMaster values (10001,'ALi')  
                 
                     Insert into #MonthDate values ('Wed','2022-11-01',null)   
                     Insert into #MonthDate values ('Thu','2022-11-02',null)   
                     Insert into #MonthDate values ('Fri','2022-11-03',null)   
                     Insert into #MonthDate values ('sat','2022-11-04',null)   
                     Insert into #MonthDate values ('Sun','2022-11-05',null)   
                     Insert into #MonthDate values ('Mon','2022-11-06',null) 
					 Insert into #MonthDate values ('Tue','2022-11-07',null)  
                     Insert into #MonthDate values ('Wed','2022-11-08',null)   
                     Insert into #MonthDate values ('Thu','2022-11-09',null)   
                     Insert into #MonthDate values ('Fri','2022-11-10',null)   
                     Insert into #MonthDate values ('Sat','2022-11-11',null)   
                     Insert into #MonthDate values ('Sun','2022-11-12',null)   
                     Insert into #MonthDate values ('Mon','2022-11-13',null) 

                          
                Insert into #tbl_Emp_Register values (10001,'2022-11-01','Wed' ,1)  
              
                Insert into #tbl_Emp_Register values (10001,'2022-11-02' ,'Thu',1)  
     
                Insert into #tbl_Emp_Register values (10001,'2022-11-03','Fri',1)  
               
				Insert into #tbl_Emp_Register values (10001,'2022-11-04','Sat',0)  

				Insert into #tbl_Emp_Register values (10001,'2022-11-05','Sun',0)  

				Insert into #tbl_Emp_Register values (10001,'2022-11-06','Mon',0)  
			
			    Insert into #tbl_Emp_Register values (10001,'2022-11-07','Tue' ,1)  
				
				Insert into #tbl_Emp_Register values (10001,'2022-11-08','Wed' ,1)  
              
                Insert into #tbl_Emp_Register values (10001,'2022-11-09' ,'Thu',1)  
     
                Insert into #tbl_Emp_Register values (10001,'2022-11-10','Fri',1)  
               
				Insert into #tbl_Emp_Register values (10001,'2022-11-11','Sat',1)  

				Insert into #tbl_Emp_Register values (10001,'2022-11-12','Sun',1)  

				Insert into #tbl_Emp_Register values (10001,'2022-11-13','Mon',0)  


User's image

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,061 questions
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-07-05T07:32:03.3033333+00:00

    Check a long variant:

    update t3 set Duty = 2
    from #tbl_Emp_Register t1
    left join #tbl_Emp_Register t2 on t2.EnrollNumbeR = t1.EnrollNumbeR and datediff(day, t1.Date, t2.Date) = 1
    left join #tbl_Emp_Register t3 on t3.EnrollNumbeR = t2.EnrollNumbeR and datediff(day, t2.Date, t3.Date) = 1
    left join #tbl_Emp_Register t4 on t4.EnrollNumbeR = t3.EnrollNumbeR and datediff(day, t3.Date, t4.Date) = 1
    left join #tbl_Emp_Register t5 on t5.EnrollNumbeR = t4.EnrollNumbeR and datediff(day, t4.Date, t5.Date) = 1
    where 
        t3.Day = 'Sat' and t3.Duty = 0 and t4.Duty = 0 and t5.Duty = 0
        or                                                                    
        t3.Day = 'Sun' and t2.Duty = 0 and t3.Duty = 0 and t4.Duty = 0
        or                                                                    
        t3.Day = 'Mon' and t1.Duty = 0 and t2.Duty = 0 and t3.Duty = 0
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.