Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to take 5 continuously absent days in sql server 2008 r2

my table like this
attid	int	Unchecked
CourseId	int	Unchecked
CourseCatID	int	Unchecked
RegNo	nvarchar(7)	Unchecked
Attdate	date	Unchecked
Attendance	char(1)	Unchecked
createUser	int	Unchecked
createdDate	date	Unchecked
updatedUser	int	Unchecked
updatedDate	date	Unchecked
statuse	int	Unchecked


my data IN THIS TABLE ARE:
3	1	1	FS001	2017-07-18	A	1	2017-07-18	1	2017-07-18	1
4	1	1	FS002	2017-07-18	P	1	2017-07-18	1	2017-07-18	1
5	1	1	FS002	2017-07-18	A	1	2017-07-18	1	2017-07-18	0
8	1	1	FS002	2017-08-01	A	1	2017-08-01	1	2017-08-01	1
9	1	1	FS002	2017-08-02	A	1	2017-08-02	1	2017-08-01	1
10	1	1	FS002	2017-08-03	A	1	2017-08-03	1	2017-08-01	1
11	1	1	FS002	2017-08-04	A	1	2017-08-04	1	2017-08-01	1
12	1	1	FS002	2017-08-05	p	1	2017-08-05	1	2017-08-01	1
13	1	1	FS002	2017-08-06	p	1	2017-08-05	1	2017-08-01	1
14	1	1	FS002	2017-08-07	p	1	2017-08-05	1	2017-08-01	1
15	1	1	FS002	2017-08-08	A	1	2017-08-05	1	2017-08-01	


I WANT CONTINUOUSLY ABSENT DAYS OF A STUDENT

What I have tried:

how to take 5 continuously absent days in sql server 2008 r2
Posted
Updated 12-Aug-17 19:59pm
v2
Comments
OriginalGriff 12-Aug-17 3:36am    
1) DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
2) And? What have *you* tried? Where are *you* stuck? what help do you need?
RickZeeland 12-Aug-17 6:20am    
Did you downvote my answer, I would like to know why.

That's a difficult one, this query is not perfect, but might get you started:
SQL
SELECT [RegNo], [Attdate]
FROM Attendence T1
WHERE [Attendance] = 'A'
AND NOT EXISTS 
  (SELECT [Attdate] from Attendence 
   WHERE DATEDIFF(d, [Attdate], T1.Attdate) BETWEEN 1 AND 4 
   AND [RegNo] = T1.[RegNo] AND [Attendance] = 'A')
AND EXISTS 
  (SELECT [Attdate] from Attendence 
   WHERE DATEDIFF(d, [Attdate], T1.Attdate) BETWEEN -4 AND -1
   AND [RegNo] = T1.[RegNo] AND [Attendance] = 'A')
 
Share this answer
 
v10
Comments
vidya jobin 14-Aug-17 6:38am    
i TRY THIS CODE .BUT I NOT GET PROPER RESULT

NOW MY TABLE DATA LIKE THIS

3 1 1 FS001 2017-07-18 A 1 2017-07-18 1 2017-07-18 1
4 1 1 FS001 2017-07-18 A 1 2017-07-18 1 2017-07-18 1
5 1 1 FS001 2017-07-18 A 1 2017-07-18 1 2017-07-18 0
8 1 1 FS001 2017-08-01 A 1 2017-08-01 1 2017-08-01 1
9 1 1 FS001 2017-08-02 A 1 2017-08-02 1 2017-08-01 1
10 1 1 FS002 2017-08-03 A 1 2017-08-03 1 2017-08-01 1
11 1 1 FS002 2017-08-04 A 1 2017-08-04 1 2017-08-01 1
12 1 1 FS002 2017-08-05 A 1 2017-08-05 1 2017-08-01 1
13 1 1 FS002 2017-08-06 A 1 2017-08-05 1 2017-08-01 1
14 1 1 FS002 2017-08-07 A 1 2017-08-05 1 2017-08-01 1
15 1 1 FS002 2017-08-08 A 1 2017-08-05 1 2017-08-01 1

I GET ANSWER LIKE THIS

FS002 2017-08-03
FS002 2017-08-04

THEN I TRY YOUR QUARRY FIRST LINES

SELECT [RegNo], [Attdate]
FROM Attendence T1
WHERE [Attendance] = 'A'
AND EXISTS
(SELECT [Attdate] from Attendence
WHERE DATEDIFF(d, [Attdate], T1.Attdate) = -1
AND [RegNo] = T1.[RegNo] AND [Attendance] = 'A')

THEN I GET
FS001 2017-08-01
FS002 2017-08-03
FS002 2017-08-04
FS002 2017-08-05
FS002 2017-08-06
FS002 2017-08-07
RickZeeland 14-Aug-17 12:55pm    
That's correct, that's why the query is not yet 'perfect', it gives overlapping results. I tested it here btw: http://sqlfiddle.com/#!6/6fb3c/1
Karthik_Mahalingam 16-Aug-17 3:35am    
my 5
Try below:

/*
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
 
Share this answer
 
I am not familiar with Sql Server but I have tested in Oracle with the minimum columns and your data. I am assuming that a student attends a single course else the query will need to be modified as 'partition by regno, courseid'.

create table courseattend
(
regno            varchar2(5) not null,
attdate          date not null,
attendance       char(1) not null
)
;
SQL



Query:

select regno, fromdate, tilldate
from
(
select regno, attdate fromdate, 
max(attdate) over 
	(partition by regno order by attdate rows between current row and 4 following) tilldate,
sum(case attendance when 'A' then 1 else 0 end) over 
	(partition by regno order by attdate rows between current row and 4 following) absentcount,
sum(1) over 
	(partition by regno order by attdate rows between current row and 4 following) numrecs
from courseattend
)
where absentcount = 5
order by 1, 2;
SQL



The expression for 'numrecs' is not really needed but I have added just to list the number of records after 'fromdate'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900