SQL如何选择具有高于值的所有值的数据

时间:2017-06-20 10:47:16

标签: sql oracle

假设我有一个名为“MARKS”的表,其中包含以下列:Value,subject_id和student_id。 现在我想写一个查询来显示所有在他们出现的所有科目中获得50多个学生的学生的姓名。 怎么能实现呢?

实施例: 让我们说主题是数学,英语,历史。 即使学生的数学成绩为100分,英语成绩为100分,但历史上有40分,他应该被认为是失败的,不能被展示。

4 个答案:

答案 0 :(得分:4)

有几种方法可以达到您的期望,但在最简单的情况下,HAVING子句可能有所帮助。在以下查询中,分组由student_id完成,因此min函数对于每个student_id的所有主题都获得最小值:

SELECT student_id
  FROM marks_table
 GROUP BY student_id
HAVING min(marks) > 50;

然后按student_id加入学生姓名。

答案 1 :(得分:1)

我会说:

 select student_id
 from table
 where student_id not in (
      select student_id 
      from table
      where value < 50
      )

请注意,如果student_id中有空值,您将收到不正确的结果。 Geta在子选择

中使用coalesce()对此进行舍入

答案 2 :(得分:0)

返回所有有科目出现的学生

select  student_id,subject_id, value from marks
 where 
 (case when value < 50 then 'failed' else 'pass' end  ) = 'pass'

答案 3 :(得分:0)

select *
from
    (
        select student_id, MIN(Value) as sum_value
        from MARKS
        group by student_id
    ) summed
where
    sum_value > 50
相关问题