2017-04-04 84 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不工作 –