连续缺勤的学生(节假日除外)

问题描述 投票:0回答:1

我想要检索 124 ID 学生,因为我想要在过去 8 天内连续缺席 4 天的学生(忽略周末)。 H. 124 学生在周末之前已连续缺席 3 天,并且在周末之后也缺席。周一 所以他就是连续缺席4天被找回来的人

我尝试了这个,效果很好,但是这个查询仅适用于MySQL 8.1,而我使用的是旧版本5.6.41-84.1和PHP版本:7.4.33。

如何在我的 MySQL 版本上执行此操作?

出勤_id 时间戳 学生id 状态
1 2023-11-05 124 P
2 2023-11-05 125 P
3 2023-11-06 124 A
4 2023-11-06 125 P
5 2023-11-07 124 A
6 2023-11-07 125 P
7 2023-11-08 124 A
8 2023-11-08 125 P
9 2023-11-09 124 H
10 2023-11-09 125 H
11 2023-11-10 124 H
12 2023-11-10 125 H
13 2023-11-11 124 A
14 2023-11-11 125 P
15 2023-11-12 124 P
16 2023-11-12 125 P
$query = $this->db->query("
select *,
    student_id,
    min(timestamp) timestamp_start,
    max(timestamp) timestamp_end
from (
    select 
        t.*, 
        row_number() over(partition by student_id order by timestamp) rn1,
        row_number() over(partition by student_id, status order by timestamp) rn2
    from attendance t
) t
where status = A AND timestamp BETWEEN (CURRENT_DATE() - INTERVAL 8 DAY) AND CURRENT_DATE()
group by student_id, rn1 - rn2
having count(*) >= 4");
mysql gaps-and-islands
1个回答
0
投票

一种方法是使用会话变量,并计算缺勤天数。重置当前的计数器,例如

select student_id, timestamp,
case when status = 'A' then @absent := @absent + 1 when status = 'P' then @absent := 0 end as absent
from attendance, (select @absent := 0) as init
where status in ('A', 'P')
order by student_id, timestamp

看到这个db-fiddle

© www.soinside.com 2019 - 2024. All rights reserved.