Badal Kumar
Попробуйте ниже:
/*
Drop table Attendence
Create table Attendence(attid int,
CourseId int,
CourseCatID int,
RegNo nvarchar(7),
Attdate date,
Attendance char(1),
createUser int,
createdDate date,
updatedUser int,
updatedDate date,
statuse int)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (1 ,1 ,1 ,'FS001', '2017-07-18', 'A', 1, '2017-07-18', 1, '2017-07-18', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (2 ,1 ,1 ,'FS002', '2017-07-18', 'P', 1, '2017-07-18', 1, '2017-07-18', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (3 ,1 ,1 ,'FS002', '2017-07-19', 'A', 1, '2017-07-18', 1, '2017-07-18', 0)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (4 ,1 ,1 ,'FS002', '2017-08-01', 'A', 1, '2017-08-01', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (5 ,1 ,1 ,'FS002', '2017-08-02', 'A', 1, '2017-08-02', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (6 ,1 ,1 ,'FS002', '2017-08-03', 'A', 1, '2017-08-03', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (7 ,1 ,1 ,'FS002', '2017-08-04', 'A', 1, '2017-08-04', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (8 ,1 ,1 ,'FS002', '2017-08-05', 'p', 1, '2017-08-05', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (9 ,1 ,1 ,'FS002', '2017-08-06', 'p', 1, '2017-08-05', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (10 ,1 ,1 ,'FS002', '2017-08-07', 'p', 1, '2017-08-05', 1, '2017-08-01', 1)
Insert into Attendence(attid,CourseId,CourseCatID,RegNo,Attdate,Attendance,createUser,createdDate,updatedUser,updatedDate,statuse) values (11 ,1 ,1 ,'FS002', '2017-08-08', 'A', 1, '2017-08-05', 1, '2017-08-01', 0)
*/
Declare @LeaveAttendence as table(Id int identity(1,1), RegNo nvarchar(7), LeaveStartDate Date, LeaveEndDate Date, ContinuousLeave int)
Declare @incrementCount int
Declare @totalCount int
Select @totalCount = Count(*) from Attendence
Select @incrementCount = Min(attid) from Attendence
Declare @RegNo nvarchar(7), @LeaveStartDate Date, @LeaveEndDate Date, @Attendance char(1)
while @incrementCount < @totalCount
Begin
Select @RegNo = RegNo, @LeaveStartDate = Attdate, @LeaveEndDate = Attdate, @Attendance = Attendance from Attendence where attid = @incrementCount
If Not Exists(Select 1 from @LeaveAttendence where @RegNo = RegNo AND ContinuousLeave < 5)
Begin
Insert into @LeaveAttendence(RegNo,LeaveStartDate,LeaveEndDate,ContinuousLeave) values (@RegNo,null,null,0)
End
if @Attendance = 'A'
Begin
print Cast(@RegNo as varchar) + ' - ' + Cast(@LeaveEndDate as varchar)
If Exists(Select 1 from @LeaveAttendence where @RegNo = RegNo AND ContinuousLeave > 0)
Begin
Update @LeaveAttendence set LeaveEndDate = @LeaveEndDate,ContinuousLeave = ContinuousLeave + 1 where @RegNo = RegNo and ContinuousLeave < 5
End
Else
Begin
Update @LeaveAttendence set LeaveStartDate = @LeaveStartDate, LeaveEndDate = @LeaveEndDate,ContinuousLeave = 1 where @RegNo = RegNo and ContinuousLeave < 5
End
End
Else
Begin
Update @LeaveAttendence set LeaveStartDate = null, LeaveEndDate = null,ContinuousLeave = 0 where @RegNo = RegNo and ContinuousLeave < 5
End
SET @incrementCount = @incrementCount + 1
END
Select * from @LeaveAttendence where ContinuousLeave > 4