【问题标题】:Write an SQL query to report the students (student_id, student_name) being “quiet” in ALL exams编写一个 SQL 查询来报告学生 (student_id, student_name) 在所有考试中“安静”
【发布时间】:2021-12-04 13:41:40
【问题描述】:

-- “相当”的学生是指至少参加过一门考试但没有得分的学生 既不是高分也不是低分。

--编写SQL查询报告学生(student_id,student_name) 在所有考试中保持“安静”。

-- 不要退回从未参加过任何考试的学生。返回结果 按 student_id 排序的表。

-- 查询结果格式如下例。

学生桌:

-- +-------------+---------------+ -- |学生ID |学生姓名 | -- +-------------+---------------+ -- | 1 |丹尼尔 | -- | 2 |翡翠 | -- | 3 |斯特拉 | -- | 4 |乔纳森 | -- | 5 |将| -- +-------------+---------------+

考试表:

-- +------------+--------------+------------+ -- |考试编号 |学生ID |分数 | -- +------------+--------------+------------+ -- | 10 | 1 | 70 | -- | 10 | 2 | 80 | -- | 10 | 3 | 90 | -- | 20 | 1 | 80 | -- | 30 | 1 | 70 | -- | 30 | 3 | 80 | -- | 30 | 4 | 90 | -- | 40 | 1 | 60 | -- | 40 | 2 | 70 | -- | 40 | 4 | 80 | -- +------------+--------------+------------+

结果表:

-- +-------------+---------------+ -- |学生ID |学生姓名 | -- +-------------+---------------+ -- | 2 |翡翠 | -- +-------------+---------------+

我的解决方案正确吗?

--我的解决方案 选择 Student_id、Student_name 从 ( 选择 B.Student_id, A.Student_name, 分数, Max(Score) Over (Partition by Exam_id) score_max, Max(Score) Over (Partition by Exam_id) score_min 从 学生 A,考试 B 在哪里 A.Student_ID = B.Student_ID ) T 在哪里 分数 != Max_score 或 Score != Min_Score 通过...分组 学生 ID,学生姓名 有 Count(*) = (从考试中选择不同的计数(exam_id)) 订购方式 A.student_id

【问题讨论】:

  • 如果你想确保你的查询是正确还是错误,那么你可以使用sql fiddles。例如:https://dbfiddle.uk/
  • 嗨@Arun。感谢您的建议。
  • “我的解决方案正确吗?” 为什么不运行它并找出答案?你的处境比我们好。
  • 进化!没有人应该使用old-style joins
  • 我不清楚这个问题:"-- 一个“相当”的学生是指至少参加过一次考试并且既没有获得高分也没有获得低分的学生。 " 这是在他们所有的考试中还是至少在他们的一项考试中?

标签: sql sql-server


【解决方案1】:

您的结果是正确的,但您需要对查询进行两次更改。

  1. 您必须在 score_min 中将 Max 更改为 Min
// ...
min(score) over (partition by exam_id) score_min,
max(score) over (partition by exam_id) score_max    
// ...
  1. 拥有应该是这样的:
having count(1) = 
(select count(distinct exam_id) from exam t2 
where t1.student_id = t2.student_id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多