【发布时间】:2020-12-29 18:34:55
【问题描述】:
我在名为 meeting 的表中有以下列:meeting_id - int、start_time - time、end_time - time。假设此表仅包含一个日历日的数据,我需要多少个最小房间数才能容纳所有会议。房间大小/参加会议的人数无关紧要。
解决办法如下:
select * from
(select t.start_time,
t.end_time,
count(*) - 1 overlapping_meetings,
count(*) minimum_rooms_required,
group_concat(distinct concat(y.start_time,' to ',t.end_time)
separator ' // ') meeting_details from
(select 1 meeting_id, '08:00' start_time, '09:15' end_time union all
select 2, '13:20', '15:20' union all
select 3, '10:00', '14:00' union all
select 4, '13:55', '16:25' union all
select 5, '14:00', '17:45' union all
select 6, '14:05', '17:45') t left join
(select 1 meeting_id, '08:00' start_time, '09:15' end_time union all
select 2, '13:20', '15:20' union all
select 3, '10:00', '14:00' union all
select 4, '13:55', '16:25' union all
select 5, '14:00', '17:45' union all
select 6, '14:05', '17:45') y
on t.start_time between y.start_time and y.end_time
group by start_time, end_time) z;
我的问题 - 这个答案有什么问题吗?即使这没有任何问题,有人可以分享更好的答案吗?
【问题讨论】:
-
@Strawberry - 谢谢。会记住这一点。
标签: mysql sql optimization