获得表中的最大记录

时间:2011-05-25 03:33:01

标签: sql aggregate-functions

我有这样的表:

表学校

ID_School     name
--------------------
ACH001        jack
ACH001        gon
ACH001        fanny
ACH001        tony
ACH002        vera
ACH002        jessica
ACH003        rey
ACH003        tomy

我希望输出此表中的最大记录ID_School,输出如下:

ID_School  count
-----------------
ACH001     4

3 个答案:

答案 0 :(得分:1)

就是这样:

select ID_school,count(ID_school) as total from school group by names order by total desc limit 1;

答案 1 :(得分:1)

在MS SQL中:

select top 1 ID_School, count(*) IdCount
from school
group by ID_School
order by IdCount desc

答案 2 :(得分:1)

对于Oracle:

SELECT id_school, cnt
  FROM (SELECT id_school, count(*) cnt
          FROM school
         GROUP BY id_school
         ORDER BY cnt)
 WHERE ROWNUM = 1;