Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用特殊条件查找DB中的字段计数_Sql_Postgresql - Fatal编程技术网

Sql 使用特殊条件查找DB中的字段计数

Sql 使用特殊条件查找DB中的字段计数,sql,postgresql,Sql,Postgresql,我的问题是: 如果我有带学生id和其他字段的表学生(无所谓) 表格考试有三个字段:学生id、科目id(来自另一个表格科目的id)和考试分数 我需要找出每个科目考试成绩相同的学生人数 我一直坚持那个任务,你们可以让我知道我该怎么做或解释完整的解决方案 简单数据: 对于此场景,查询必须返回count(student_id)=4您需要嵌套聚合: select count(*) from ( select student_id from tab group by student_i

我的问题是: 如果我有带学生id和其他字段的表学生(无所谓) 表格考试有三个字段:学生id、科目id(来自另一个表格科目的id)和考试分数

我需要找出每个科目考试成绩相同的学生人数

我一直坚持那个任务,你们可以让我知道我该怎么做或解释完整的解决方案

简单数据:


对于此场景,查询必须返回count(student_id)=4

您需要嵌套聚合:

select count(*)
from
 (
   select student_id
   from tab
   group by student_id
   -- same min and max means only one value exists
   having min(exam_mark) = max(exam_mark)
 ) as dt
最小/最大值通常比
计数(不同的考试分数)=1更有效

如果你只想让多个分数的学生提高

    AND count(*) > 1

请提供示例数据和所需结果。@GordonLinoff我编辑了这个问题