sql HAVING max(count()) return zero rows

做~自己de王妃 提交于 2021-02-05 06:48:46

问题


I'm trying to get class rooms with overlap course schedule, my tables: courses:

COURSE_ID    NAME
11           matematika
22           logika
33           himiya
44           sport
55           algoritmika
66           hedva
77           algebra linearit

schedule:

ID  COURSE_ID  ID_ROOM  DAY  HOUR
1   11         105      Mon  10am
2   11         105      Wen  10am
3   11         105      Thu  10am
4   22         105      Mon  10am
5   22         205      Wen  10am
6   22         105      Thu  10am
7   33         305      Mon  11am
8   33         105      Mon  10am

class_room:

ID_ROOM  LOCATION  CAPACITY
105      A         20
205      B         10
305      C         30

My sql is:

select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h,  count(courses.COURSE_ID) as count 
  from schedule
  natural join class_room
  natural join courses
  group by crid, d, h
  order by count desc;

and I get:

crid  LOCATION  d   h       count
105   A         Mon 10am    3
105   A         Thu 10am    2
305   C         Mon 11am    1
105   A         Wen 10am    1
205   B         Wen 10am    1

But I need to show all maximal values of count only (1 such row for now). I tryed

select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h,  count(courses.COURSE_ID) as count 
  from schedule
  natural join class_room
  natural join courses
  group by crid, d, h
  having max(count)
  order by count desc;

But is return empty table. What is wrong? Or, maybe suggestion of another solution, to get what I need?


回答1:


Following would return all groups matching the max count

SQL Fiddle

select  class_room.ID_ROOM as crid
        , class_room.LOCATION
        , schedule.DAY as d
        , schedule.HOUR as h
        ,  count(courses.COURSE_ID) as count 
from    schedule
        natural join class_room
        natural join courses
group by 
        crid, d, h
having count(*) = (
                    select  max(count)
                    from    (            
                              select  count(courses.COURSE_ID) as count
                              from    schedule
                                      natural join class_room
                                      natural join courses
                              group by 
                                      id_room, day, hour
                            ) maxcount
                    )



回答2:


This will return all rows with the highest count:

select ID_ROOM as crid, DAY as d, HOUR as h,
      count(*) as cnt
from schedule
group by crid, d, h
having
   count(*)
   = ( select count(*) as cnt
       from schedule
       group by ID_ROOM, DAY, HOUR
       order by cnt desc
       limit 1
     )

Now join this to class_room to get the location



来源:https://stackoverflow.com/questions/28524825/sql-having-maxcount-return-zero-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!