Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mysql - 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:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
                    )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...