group by in sql to find max count -


update have query

select sl.college_id,sl.department_id,count(sl.registernumber)  studentlist sl group  sl.college_id,sl.department_id order sl.college_id,sl.department_id asc 

abouve query gives below result , have 200 - college id , each college have 6 department_id count [no.of student ] in each department

college_id  dept_id  count  1           1           100 1           2           210 2           3           120 2           6           80 3           1           340 

but question need display maximum count[student] each department

some thing

college_id  dept_id  count  3               1       340 26              2       250 

and tried out getting error

select sl.college_id,sl.department_id,count(sl.registernumber)  studentlist sl group  sl.college_id,sl.department_id having  count(sl.registernumber)=max(count(sl.registernumber)) order sl.college_id,sl.department_id asc 

what went wrong can 1 me

maybe this?

select sl.college_id, sl.department_id, count(sl.registernumber) studentcount, s2.maxcount     studentlist sl     inner join (         select department_id, max(studentcount) maxcount             (                 select college_id, department_id, count(*) studentcount                     studentlist                     group college_id, department_id                 ) s1             group department_id         ) s2 on sl.department_id = s2.department_id     group sl.college_id, sl.department_id, s2.maxcount     having count(sl.registernumber) = s2.maxcount     order sl.college_id, sl.department_id asc 

edit: i've updated query more accurately answer question, missed part want college_id max count.

edit 2: okay, should work now, needed second nested subquery aggregating aggregates. don't know of better way compare aggregates of different groups.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -