【问题标题】:Mysql how to select count aggregation results at 10-minute intervalsMysql如何以10分钟间隔选择计数聚合结果
【发布时间】:2021-11-11 01:57:42
【问题描述】:

我想每 10 分钟查询一次特定列的计数,直到当前时间。

假设实际数据如下。

| access_no |     in_datetime     |
-----------------------------------
|   1       | 2021-09-16 09:10:30 |
|-----------|---------------------|
|   2       | 2021-09-16 09:15:05 |
|-----------|---------------------|
|   3       | 2021-09-16 10:03:23 |
|-----------|---------------------|
|   4       | 2021-09-16 11:13:53 |
|-----------|---------------------|
|   5       | 2021-09-16 11:30:10 |
|-----------|---------------------|

此时想要的输出结果如下。


| hh:mm     | au  |
-------------------
|   09:10   |  2  |
|-----------|-----|
|   09:20   |  0  |
|-----------|-----|
|   09:30   |  0  |
|-----------|-----|
|   09:40   |  0  |
|-----------|-----|
|   09:50   |  0  |
|-----------|-----|
|   10:00   |  1  |
|-----------|-----|
|   10:10   |  1  |
|-----------|-----|
|   10:20   |  0  |
|-----------|-----|
|   10:30   |  1  |
|-----------|-----|

到目前为止,我所做的查询如下。

SELECT
   case 
        when substr(in_datetime,15,2) < '10' then concat(substr(in_datetime,12,2),':00')
        when substr(in_datetime,15,2) >='10' and substr(in_datetime,15,2)<'20' then concat(substr(in_datetime,12,2),':10')
      when substr(in_datetime,15,2) >='20' and substr(in_datetime,15,2)<'30' then concat(substr(in_datetime,12,2),':20')
      when substr(in_datetime,15,2) >='30' and substr(in_datetime,15,2)<'40' then concat(substr(in_datetime,12,2),':30')
      when substr(in_datetime,15,2) >='40' and substr(in_datetime,15,2)<'50' then concat(substr(in_datetime,12,2),':40')
      when substr(in_datetime,15,2) >='50' and substr(in_datetime,15,2)<'60' then concat(substr(in_datetime,12,2),':50')
   END as hhmm,
   count(access_no) as au
FROM tbl_access

where date(out_datetime) = str_to_date('20210916', '%Y%m%d')
and in_datetime is not null

group by substr(in_datetime,1,10) ,
   case 
        when substr(in_datetime,15,2)<'10' then concat(substr(in_datetime,12,2),':00')
        when substr(in_datetime,15,2) >='10' and substr(in_datetime,15,2)<'20' then concat(substr(in_datetime,12,2),':10')
      when substr(in_datetime,15,2) >='20' and substr(in_datetime,15,2)<'30' then concat(substr(in_datetime,12,2),':20')
      when substr(in_datetime,15,2) >='30' and substr(in_datetime,15,2)<'40' then concat(substr(in_datetime,12,2),':30')
      when substr(in_datetime,15,2) >='40' and substr(in_datetime,15,2)<'50' then concat(substr(in_datetime,12,2),':40')
      when substr(in_datetime,15,2) >='50' and substr(in_datetime,15,2)<'60' then concat(substr(in_datetime,12,2),':50')
   END

但是,在这个查询的结果中,没有检索到没有数据的时间的值,如下图所示。


| hh:mm     | au  |
-------------------
|   09:10   |  2  |
|-----------|-----|
|   10:00   |  1  |
|-----------|-----|
|   10:10   |  1  |
|-----------|-----|
|   10:30   |  1  |
|-----------|-----|

如果到当前时间还没有数据,如何找回时间?

谢谢大家!

【问题讨论】:

    标签: mysql sql select mariadb


    【解决方案1】:

    如果我正确理解了您的问题,那么这可以通过日历表来解决。

    您可以使用recursive cte 创建时间列表,然后根据现有查询使用DATE_FORMAT() 对其进行格式化以仅显示hh:mm

    with recursive times as (
        select '1970-01-01 00:00' t 
        union all
        select t + interval 10 minute 
        from times 
        where t  <= '1970-01-01 23:40'
     )
     select 
         DATE_FORMAT(t, '%H:%i') 'hh:mm'
     from 
         times
    

    输出:

    hh:mm
    00:00
    00:10
    00:20
    ...

    然后,您可以将其加入到您现有的查询中,每次都返回一行。

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      相关资源
      最近更新 更多