【发布时间】: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 |
|-----------|-----|
如果到当前时间还没有数据,如何找回时间?
谢谢大家!
【问题讨论】: