使用MAX()子查询进行COUNT(*)

时间:2015-06-09 02:53:23

标签: sql oracle

我正在尝试构建一个语句,该first_namelast_namecourse_nodescription表示instructor teaches max最java的课程。我的SELECT first_name, last_name, course_no, description FROM( SELECT i.first_name, i.last_name, c.course_no, c.description FROM instructor i INNER JOIN section s ON i.instructor_id = s.instructor_id JOIN course c ON c.course_no = s.course_no WHERE c.description LIKE '%Java%') GROUP BY first_name, last_name, course_no, description HAVING COUNT(*) = (SELECT MAX(course_count) FROM (SELECT DISTINCT first_name, last_name, COUNT(*) As course_count FROM( SELECT i.first_name, i.last_name, c.course_no, c.description FROM instructor i INNER JOIN section s ON i.instructor_id = s.instructor_id JOIN course c ON c.course_no = s.course_no WHERE c.description LIKE '%Java%') GROUP BY first_name, last_name)); 语句显示了最大课程数,但是当我执行完整语句时,我没有得到任何结果。 以下是查询:

doc.css(".row, .row_alternate").each do |a_row|
  # do stuff with a_row
end

1 个答案:

答案 0 :(得分:1)

您的外部查询与HAVING子句中的子查询不匹配:前者有GROUP BY first_name, last_name, course_no, description,后者只有GROUP BY first_name, last_name。当然,您通常不会发现前列的任何4元组,其行数与后面列中最常见的一对一样。