2017-04-04 52 views
0

的输出最大结果我使用的查询:查询来获取来自其他查询

select count(registration.stu_id) as cnt,course.dept_id from registration,course where registration.course_id=course.course_id group by registration.course_id, course.dept_id 

从该查询我得到的结果是:

cnt | dept_id 
2  | 1 
1  | 3 
1  | 2 
1  | 4 
1  | 5 

现在我需要找出与结果max cnt

我应该使用什么查询?

回答

0

下面的查询使用count(registration.stu_id)as cnt,course.dept_id from registration,course where registration.course_id = course.course_id group by registration.course_id,course.dept_id 为了通过CNT递减限制使用子查询尽可能获得更好的性能1

避免..

+0

限制不支持在MS SQL服务器 –

+0

是SQL Server不支持限制,您可以改为使用顶部关键字。你在mysql中标记了这个问题。 – Krishna

0
SELECT MAX(cnt) FROM (
select count(registration.stu_id) as cnt,course.dept_id 
from registration,course where registration.course_id=course.course_id group 
by registration.course_id, course.dept_id) 
y 

您可以使用查询作为子查询,然后获得最大

+0

我尝试过,但在MS SQL Server –

0

,如果你想获得最高1最高值

选择尝试这一个

select max(cnt) as max_cnt 
from (select count(registration.stu_id) as cnt, 
      course.dept_id 
     from registration,course 
     where registration.course_id=course.course_id 
     group by registration.course_id, course.dept_id) 
+0

不工作在MS SQL Server不工作 –