【问题标题】:Get all occurences of a time range between another time range获取一个时间范围在另一个时间范围之间的所有出现
【发布时间】:2022-01-30 22:41:05
【问题描述】:

我有一个设置了日期以及开始和结束时间的数据库

date start end
2021-12-04 10:15:00 13:05:00
2021-12-04 12:18:00 15:38:00
... ... ...

这张桌子可以节省人们进出的时间。

对于一个统计数据,我想获取每小时打卡的人数

例如,如果我指定日期“2021-12-04”,表格应如下所示:

hour people
10:00-10:59 1
11:00-11:59 1
12:00-12:59 2
13:00-13:59 2
14:00-14:59 1
15:00-15:59 1

我一直在尝试一些查询,我得到的表格在一小时内正确返回了数字,但是一旦涉及到每个小时有人打卡,我似乎无法找到按预期工作的解决方案

【问题讨论】:

  • 哪个版本的 MySQL?

标签: mysql sql


【解决方案1】:

带小时生成器:

select hour, count(date) as people
from (select concat(lpad(@num:=@num+1,2,'0'),':00-',lpad(@num,2,'0'),':59') as hour
    from information_schema.tables t1,
        (select @num:=-1) tmp
        limit 24) a
left join table1 b
    on  substr(a.hour,1,2) >= substr(b.start,1,2)
    and substr(a.hour,1,2) <= substr(b.end,1,2)
group by 1

或每小时检查一次,结合联合:

select '00:00-00:59' as hour, count(date) as people from table1
 where '00' between substr(start,1,2) and substr(end,1,2) union
select '01:00-01:59' as hour, count(date) as people from table1
 where '01' between substr(start,1,2) and substr(end,1,2) union
select '02:00-02:59' as hour, count(date) as people from table1
 where '02' between substr(start,1,2) and substr(end,1,2) union
select '03:00-03:59' as hour, count(date) as people from table1
 where '03' between substr(start,1,2) and substr(end,1,2) union
select '04:00-04:59' as hour, count(date) as people from table1
 where '04' between substr(start,1,2) and substr(end,1,2) union
select '05:00-05:59' as hour, count(date) as people from table1
 where '05' between substr(start,1,2) and substr(end,1,2) union
select '06:00-06:59' as hour, count(date) as people from table1
 where '06' between substr(start,1,2) and substr(end,1,2) union
select '07:00-07:59' as hour, count(date) as people from table1
 where '07' between substr(start,1,2) and substr(end,1,2) union
select '08:00-08:59' as hour, count(date) as people from table1
 where '08' between substr(start,1,2) and substr(end,1,2) union
select '09:00-09:59' as hour, count(date) as people from table1
 where '09' between substr(start,1,2) and substr(end,1,2) union
select '10:00-10:59' as hour, count(date) as people from table1
 where '10' between substr(start,1,2) and substr(end,1,2) union
select '11:00-11:59' as hour, count(date) as people from table1
 where '11' between substr(start,1,2) and substr(end,1,2) union
select '12:00-12:59' as hour, count(date) as people from table1
 where '12' between substr(start,1,2) and substr(end,1,2) union
select '13:00-13:59' as hour, count(date) as people from table1
 where '13' between substr(start,1,2) and substr(end,1,2) union
select '14:00-14:59' as hour, count(date) as people from table1
 where '14' between substr(start,1,2) and substr(end,1,2) union
select '15:00-15:59' as hour, count(date) as people from table1
 where '15' between substr(start,1,2) and substr(end,1,2) union
select '16:00-16:59' as hour, count(date) as people from table1
 where '16' between substr(start,1,2) and substr(end,1,2) union
select '17:00-17:59' as hour, count(date) as people from table1
 where '17' between substr(start,1,2) and substr(end,1,2) union
select '18:00-18:59' as hour, count(date) as people from table1
 where '18' between substr(start,1,2) and substr(end,1,2) union
select '19:00-19:59' as hour, count(date) as people from table1
 where '19' between substr(start,1,2) and substr(end,1,2) union
select '20:00-20:59' as hour, count(date) as people from table1
 where '20' between substr(start,1,2) and substr(end,1,2) union
select '21:00-21:59' as hour, count(date) as people from table1
 where '21' between substr(start,1,2) and substr(end,1,2) union
select '22:00-22:59' as hour, count(date) as people from table1
 where '22' between substr(start,1,2) and substr(end,1,2) union
select '23:00-23:59' as hour, count(date) as people from table1
 where '23' between substr(start,1,2) and substr(end,1,2)

【讨论】:

    【解决方案2】:

    简洁的 w col1,将与:

    select substr(start, 1, 2) as hour
        , count(start) as people
    from table1
    group by 1
    order by 1
    

    以完整的小时符号为例:

    select concat(substr(start, 1, 2), ':00-', substr(start, 1, 2), ':59') as hour
        , count(start) as people
    from table1
    group by 1
    order by 1
    

    dbfiddle here (MySQL 8.0.27)

    【讨论】:

    • 在示例中,第一行从上午 10 点开始,到下午 1 点结束。这意味着它需要为四个不同的输出行做出贡献;上午 10 点、上午 11 点、下午 12 点和下午 1 点
    猜你喜欢
    • 2022-01-03
    • 2020-06-05
    • 1970-01-01
    • 2012-08-05
    • 2017-02-10
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多