用于识别连续5个上学日缺席的学生的SQL查询

时间:2017-10-25 13:24:24

标签: mysql

使用MySQL 2012,我需要使用记录出勤率的学生信息系统识别连续5天或以上缺课的学生。

我有'Day'表,其中包含一年中上学日的所有日期(不包括周末,假日等),我构建了一个临时表来保存我想要的值。

我没有得到如何格式化所有这些,我希望它是可读的!

SELECT *
INTO #GetAttDates
FROM
(
SELECT convert(date,Date)AS AttendanceDate 
FROM Day
WHERE calendarID = 2062
AND attendance = 1
)x

输出摘录:

AttendanceDate
8/30/2017
8/31/2017
9/1/2017
9/5/2017
9/6/2017
9/7/2017
9/8/2017
9/11/2017
9/12/2017
9/13/2017
9/14/2017
9/15/2017

我认为我可以为每个学生提供缺席。我从视图中构建了几个临时表 - 以这个临时表结束,让我可以将日期与学校的“Day”表进行比较。

SELECT *
INTO #TempAttendanceThree
FROM
(
select personID
, wholeDayAbsence
, halfDayAbsence
, currDate
, status
, excuse
, description
, case when netMinutes < halfDayAbsence  Then 0
    when (netMinutes >= halfDayAbsence) and (netMinutes < wholeDayAbsence) then .5
  else 1
  end as AbDays
from #TempAttendanceTwo 
where case when netMinutes < halfDayAbsence  Then 0
    when (netMinutes >= halfDayAbsence) and (netMinutes <     wholeDayAbsence) then .5
  else 1
end = 1
group by personID
, wholeDayAbsence
, halfDayAbsence
, currDate
, status
, excuse
, description
, netMinutes
)x

输出:

personID    wholeDayAbsence halfDayAbsence  currDate    status  excuse  description AbDays
89  265 156 9/18/2017   A   E   Absent Excused  1
89  265 156 10/5/2017   A   E   Absent Excused  1
89  265 156 10/16/2017  A   X   Field Trip  1
537 265 156 9/6/2017    A   E   Dismissed   1
18889   265 156 9/12/2017   A   U   Absent Unexcused    1
18889   265 156 9/13/2017   A   U   Absent Unexcused    1
18889   265 156 9/14/2017   A   U   Absent Unexcused    1
18889   265 156 9/19/2017   A   U   Absent Unexcused    1
18889   265 156 9/20/2017   A   U   Absent Unexcused    1
18889   265 156 9/22/2017   A   U   Absent Unexcused    1
18889   265 156 9/26/2017   A   U   Absent Unexcused    1
18889   265 156 9/27/2017   A   U   Absent Unexcused    1
18889   265 156 9/28/2017   A   U   Absent Unexcused    1
18889   265 156 9/29/2017   A   U   Absent Unexcused    1
18889   265 156 10/2/2017   A   U   Absent Unexcused    1
18889   265 156 10/3/2017   A   U   Absent Unexcused    1
18889   265 156 10/4/2017   A   U   Absent Unexcused    1
18889   265 156 10/5/2017   A   U   Absent Unexcused    1
18889   265 156 10/10/2017  A   U   Absent Unexcused    1
18889   265 156 10/11/2017  A   U   Absent Unexcused    1
18889   265 156 10/12/2017  A   U   Absent Unexcused    1
18889   265 156 10/13/2017  A   U   Absent Unexcused    1
18889   265 156 10/16/2017  A   U   Absent Unexcused    1
18889   265 156 10/17/2017  A   U   Absent Unexcused    1
18889   265 156 10/18/2017  A   U   Absent Unexcused    1
18889   265 156 10/19/2017  A   U   Absent Unexcused    1
18889   265 156 10/20/2017  A   U   Absent Unexcused    1
18889   265 156 10/23/2017  A   U   Absent Unexcused    1
18889   265 156 10/24/2017  A   U   Absent Unexcused    1

这让我陷入了困境。

如何读取一个personID的记录,并将日期与“日期”表日期进行比较,以查看是否连续有5个或更多缺席。

显然,ID#18889应被视为符合该标准。

我设想在比较缺席日期与“出席日期”之后,在幕后工作的比较:

AttendanceDate  89  537 18889
8/30/2017            
8/31/2017            
9/1/2017             
9/5/2017             
9/6/2017        9/6/2017     
9/7/2017             
9/8/2017             
9/11/2017            
9/12/2017           9/12/2017
9/13/2017           9/13/2017
9/14/2017           9/14/2017
9/15/2017            
9/18/2017   9/18/2017        
9/19/2017           9/19/2017
9/20/2017           9/20/2017
9/21/2017           9/22/2017
9/22/2017            
9/25/2017            
9/26/2017           9/26/2017
9/27/2017           9/27/2017
9/28/2017           9/28/2017
9/29/2017           9/29/2017
10/2/2017           10/2/2017
10/3/2017           10/3/2017
10/4/2017           10/4/2017
10/5/2017   10/5/2017       10/5/2017
10/10/2017          10/10/2017
10/11/2017          10/11/2017
10/12/2017          10/12/2017
10/13/2017          10/13/2017
10/16/2017  10/16/2017      10/16/2017
10/17/2017          10/17/2017
10/18/2017          10/18/2017
10/19/2017          10/19/2017
10/20/2017          10/20/2017
10/23/2017          10/23/2017
10/24/2017          10/24/2017

我需要一个查询日期(按personID)并输出结果,如下所示:

personID    status  Consecutive Days
89  A   1
537 A   1
18889   A   19

1 个答案:

答案 0 :(得分:0)

一个想法是在您的出勤表上连续出示身份证明......

ID AttendanceDate
1  8/30/2017
2  8/31/2017
3  9/1/2017
4  9/5/2017
5  9/6/2017
6  9/7/2017
7  9/8/2017
8  9/11/2017
9  9/12/2017
10 9/13/2017
11 9/14/2017
12 9/15/2017

然后你可以建立5天的范围

SELECT A1.AttendanceDate as range_start, 
       A2.AttendanceDate as range_end
FROM Attendance A1    
JOIN Attendance A2
  ON A1.ID = A2.ID - 5

现在,您可以检查您的学生在该范围内是否有超过5人缺席

 SELECT range_start, range_end, personID
 FROM yourPersonTable
 JOIN ( SELECT A1.AttendanceDate as range_start, 
               A2.AttendanceDate as range_end
        FROM Attendance A1    
        JOIN Attendance A2
          ON A1.ID = A2.ID - 5) as Q
   ON yourPersonTable.currDate BETWEEN range_start AND range_end
 GROUP BY range_start, range_end, personID
 HAVING COUNT(CASE WHEN excuse = 'Unexcused' THEN 1 END) >= 5

请注意您的架构不清楚,所以我在那里做一些猜测