简体   繁体   English

SQL 找到最大计数

[英]SQL find Max of count

I'm trying to get the unitcode from this我正在尝试从中获取单位代码

SELECT
    max(maxcount) 
FROM(
        SELECT
            COUNT(studid) maxcount,
            semester,
            e.unitcode,
            u.unitname,
            to_char(ofyear, 'yyyy')
        FROM
            uni.enrolment e Join uni.unit u 
        ON 
            e.unitcode = u.unitcode
        WHERE
            to_char(ofyear, 'yyyy') = '2013'
        GROUP BY
            semester,
            e.unitcode,
            u.unitname,
            to_char(ofyear, 'yyyy')
    );

But I'm unable to retrieve it.但我无法找回它。 I'm looking for the max value corresponding to the unit code.我正在寻找与单位代码对应的最大值。

If you want the max per unit code, then use aggregation:如果您想要每单位代码的最大值,请使用聚合:

SELECT unitcode, MAX(maxcount) 
FROM (SELECT COUNT(studid) as maxcount, semester, e.unitcode, u.unitname
      FROM uni.enrolment e JOIN
           uni.unit u 
           ON  e.unitcode = u.unitcode
      WHERE ofyear >= DATE '2013-01-01' AND ofyear < DATE '2014-01-01'
      GROUP BY semester, e.unitcode, u.unitname
     ) eu
GROUP BY unitcode;

Based on your comment, you want:根据您的评论,您希望:

SELECT COUNT(studid) as maxcount, semester, e.unitcode, u.unitname
FROM uni.enrolment e JOIN
     uni.unit u 
     ON e.unitcode = u.unitcode
WHERE ofyear >= DATE '2013-01-01' AND ofyear < DATE '2014-01-01'
GROUP BY semester, e.unitcode, u.unitname
ORDER BY maxcount DESC
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM