我的任务是获取当月(2023 年 11 月)参加过考试且在过去 12 个月内至少参加过一次考试的普通学生。所以回顾期是12个月。
还有一个条件是学生在之前的尝试中应该获得至少 20 分。
尝试次数可以 >= 1。主题并不重要。
Roll_number
是加入条件。
这是我尝试过的查询:
select
roll_number, substr(Exam_Date, 1, 7) yr_mon,
count(roll_number) as total_count
from
students_results
where
exam_date between '2022-11-30' and '2023-11-30'
and marks > 20
and exists (select roll_number
from students_results
where marks > 20
group by roll_number
having count(SupplierID) >= 1);
样本数据:
卷数 | 考试科目 | 考试_日期 | 标记 |
---|---|---|---|
100 | 英语 | 2023-08-01 | 30 |
100 | 科学 | 2023-09-05 | 50 |
100 | 英语 | 2023-11-15 | 80 |
101 | 英语 | 2021-04-01 | 65 |
101 | 科学 | 2023-06-10 | 45 |
101 | 英语 | 2023-11-17 | 85 |
102 | 英语 | 2021-04-01 | 70 |
102 | 数学 | 2023-09-01 | 15 |
102 | 数学 | 2023-11-19 | 60 |
104 | 英语 | 2023-05-01 | 40 |
104 | 英语 | 2023-11-03 | 75 |
105 | 数学 | 2023-04-01 | 10 |
105 | 英语 | 2023-11-14 | 80 |
所以,我们期望得到
Roll_number
:100, 101, 104
我正在尝试学习编写自连接查询。
首选语言:HiveQL/MySQL
如有任何帮助,我们将不胜感激。
因为这看起来像作业
我们正在寻找的是记录
FROM
样本数据
WHERE
考试日期大于当前日期减去一个月 (https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-sub )
AND
有EXISTS
(https://www.w3schools.com/mysql/mysql_exists.asp)表中存在相同的roll_number和科目的另一条记录
WHERE
exam_date 较早
AND
exam_date 距当前日期已超过 1 年
AND
分数大于20