简体   繁体   English

sql HAVING max(count()) 返回零行

[英]sql HAVING max(count()) return zero rows

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:我的sql是:

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).但我只需要显示计数的所有最大值(现在有 1 个这样的行)。 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?或者,也许是另一种解决方案的建议,以获得我需要的东西?

Following would return all groups matching the max count以下将返回匹配最大计数的所有组

SQL Fiddle SQL小提琴

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
                    )

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现在将其加入class_room以获取location

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

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