【问题标题】:How to find a cumulative absent dates from a list of school dates如何从学校日期列表中查找累积缺勤日期
【发布时间】:2017-10-23 21:01:52
【问题描述】:

我有代码可以从我们的上课日表中提取“出勤”日期(排除节假日、周末、研讨会日等)。

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
9/18/2017
9/19/2017
9/20/2017
9/21/2017
9/22/2017
9/25/2017
9/26/2017
9/27/2017
9/28/2017
9/29/2017
10/2/2017
10/3/2017

我有构建临时表的代码,可以为我提供学生 ID 列表和他们缺席的日期。

studentID | currDate
89        | 9/18/2017
89        | 10/5/2017
89        | 10/16/2017
537       | 9/6/2017
541       | 9/11/2017
541       | 9/27/2017
549       | 9/18/2017
549       | 9/20/2017
549       | 9/28/2017
549       | 9/29/2017
549       | 10/2/2017
549       | 10/3/2017
549       | 10/4/2017
549       | 10/5/2017
549       | 10/10/2017
550       | 10/19/2017
845       | 9/26/2017
845       | 10/2/2017
897       | 10/5/2017
897       | 10/20/2017
990       | 9/22/2017
990       | 9/26/2017
990       | 9/27/2017
990       | 9/28/2017
990       | 10/3/2017

我想要对每个 studentID 做的是比较他们的缺勤日期和上学日表的出勤日期,并找出累计缺勤情况。我一直在绞尽脑汁想看看如何做到这一点(临时表、游标等),但一直无法弄清楚。

我通过 AquaData Studio 使用 MySQL 2012。有任何想法吗?最后,我想要一份报告,列出累计缺勤超过 5 次的学生名单。谢谢!

【问题讨论】:

标签: mysql sql


【解决方案1】:

如果你想缺席五次:

select s.studentId, count(*)
from tempstudents s
group by s.studentId
having count(*) >= 5;

但是,我怀疑您打算在AttendanceDates 连续 5 天。首先,添加一列来枚举出勤日期:

alter table AttendanceDates add id int;

set @id = 0;
update AttendanceDates
    set id = (@id := @id + 1)
    order by AttendanceDate;

然后,您想连续查找五个 id。这是一种方法:

select studentid, count(*), min(currdate), max(currdate)
from (select ads.*,
             (@rn := if(@s = studentid, @rn + 1, if(@s := studentid, 1, 1))) as rn
      from (select s.*, ad.id
            from AttendanceDates ad join
                 tempstudents s
                 on s.currdate = ad.AttendanceDate
            order by s.studentid, ad.id
           ) ads cross join
           (select @s := 0, @rn := 0) params
     ) ads
group by s.studentid, (id - @rn)
having count(*) >= 5;

这使用变量来枚举当前日期。然后它与id 不同,后者标识缺勤序列。外部聚合只是简单地计算这些得到 5。

【讨论】:

  • 天啊——我很抱歉。我有两种类型的缺勤; (1) 累积和 (2) 连续。我在这里用错了词。如上所述,累积很容易,但连续的出勤日期是困难的,也是我苦苦挣扎的地方。我将浏览上面的代码,看看它是否对我有帮助,我非常感谢您的反馈。
  • @Karen。 . .只能回答您提出的问题。我建议您用更合适的样本数据和期望的结果问 另一个 问题。编辑此问题会使答案无效。
  • 我明白了,谢谢戈登。我会这样做,并在提出问题时做得更好。 :)
  • 我选择此回复作为答案,因为您确实回答了我错误地提出的问题。我正在发布另一个问题,以针对连续缺勤提供更多信息。谢谢。
  • 戈登,如果你有机会,如果你看到这个 - 你能告诉我冒号在@rn := if ...... 它在我尝试使用时会引发错误它。谢谢
【解决方案2】:

这可能就是你想要的:

SQL Fiddle

MySQL 5.6 架构设置

查询 1

SELECT studentID
FROM absent
WHERE currDate IN (
    SELECT AttendanceDate 
    FROM school_days
  )
GROUP BY studentID
HAVING count(*) >= 5

Results

| studentID |
|-----------|
|       549 |
|       990 |

【讨论】:

  • 为什么您需要查看 AttendanceDate?算上缺席还不够吗?
  • @JuanCarlosOropeza 如果 OP 提供了 AttendanceDate,我希望您可以在不需要出席的当天缺勤(我们可以只检查一门课程,或者我不知道)。但是是的,如果我们只想要缺席超过 5 次的学生,我们可以删除 WHERE IN
  • 不需要出勤不能缺席。 ;)
  • @JuanCarlosOropeza 我至少可以想到 3 种方法:您提供缺勤的日期跨度 (from / to),无论您是否需要出勤,都会记录每天的记录;您使用完整的缺勤表,但只想要特定课程的出勤率;您有可选课程,即使该课程不是强制性的(针对法律问题),也会将您标记为“缺席”;)
  • 我很抱歉在这里使用了错误的词 - 这是我追求的连续缺席。州要求报告连续 5 天缺课的学生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 2019-06-21
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多