sql - MySQL; Return most frequent occurrence(s) where there may be more than one -
i have table student ids (sid) , classes (ccode) they're taking.
i'm trying retrieve student ids of student(s) taking highest number of classes, query needs allow fact there might tie.
i know highest number of occurrences of same sid indicate the 1 i'm looking if looking retrieve top record i'd go
mysql> select sid, count(sid) numberofclasses -> student_classes -> group sid -> order numberofclasses desc -> limit 1; +------+-----------------+ | sid | numberofclasses | +------+-----------------+ | 2040 | 3 | +------+-----------------+
i've tried
mysql> select sid, count(sid) numberofclasses -> student_classes -> group sid -> having numofclasses=3; +------+-----------------+ | sid | numberofclasses | +------+-----------------+ | 2040 | 3 | | 3040 | 3 | +------+-----------------+
which works because know value of highest number of projects working on 3, need way of putting max() function after = calculate highest value in numberofprojects column of newly created table….
any thoughts?
this original student_classes table…
mysql> select * student_classes; +------+-------+ | sid | ccode | +------+-------+ | 2040 | c101 | | 2040 | c102 | | 2040 | c103 | | 3282 | c101 | | 3282 | c102 | | 2113 | c105 | | 3040 | c102 | | 3040 | c105 | | 3040 | c108 | +------+-------+
have tried combining 2 approaches using subquery?
select sid, count(sid) numberofclasses student_classes group sid having count(sid) = (select count(sid) n student_classes group sid order n desc limit 1)
Comments
Post a Comment