Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql按时间间隔按天计数记录_Mysql_Sql - Fatal编程技术网

Mysql按时间间隔按天计数记录

Mysql按时间间隔按天计数记录,mysql,sql,Mysql,Sql,我现在有一个大约200万或300万的事件表,开始和结束日期分布了几年。 我想知道每天有多少活动。不是以天开始,而是在每个日历日发生。 例如 预期结果: | day | # | | 2019/01/01 | 1 | | 2019/01/02 | 2 | | 2019/01/03 | 2 | | 2019/01/04 | 1 | | 2019/01/05 | 0 | | ... | 0 | | 2019/02/22 | 1 | | 2019/02/23 | 1 | 这很

我现在有一个大约200万或300万的事件表,开始和结束日期分布了几年。 我想知道每天有多少活动。不是以天开始,而是在每个日历日发生。 例如

预期结果:

| day        | # |
| 2019/01/01 | 1 |
| 2019/01/02 | 2 |
| 2019/01/03 | 2 |
| 2019/01/04 | 1 |
| 2019/01/05 | 0 |
|     ...    | 0 |
| 2019/02/22 | 1 |
| 2019/02/23 | 1 |

这很棘手。在MySQL中,从日期开始并使用相关子查询:

select d.dte, count(e.start) as cnt
from (select date('2019-01-01') as dte union all
      select date('2019-01-02') as dte union all
      select date('2019-01-03') as dte union all
      select date('2019-01-04') as dte
     ) d left join
     events e
     on e.start <= d.dte and d.dte <= e.end
group by d.dte
order by d.dte;

为这类信息设置日历表是可行的。那么

select c_date, count(*)
from calendar c
inner join events on e.start <= c_date and c_date <= e.end
group by c_Date;

试试这样,你需要一个日期生成器

select
    d.dte, count(e.start) as cnt
from 
(
select dte from
    (select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) dte from
    (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
    (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
    (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
    (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
    (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
Where dte between '2019-01-01' and '2020-12-31'
) d
inner join events e
     on e.start <= d.dte and d.dte <= e.end
group by d.dte

顺便说一句,当用几个日期来描述示例时,通常最清楚的是使用12号之后的日期。这似乎是显而易见的,但需要经常指出。此外,为了补充下面提到的——对于在给定日期没有事件发生的情况,考虑在应用程序代码中处理这个问题。@草莓,这正是为什么我从一年开始使用日期的原因。我几乎可以肯定,只有一种格式是这样的。我更新了这个问题,以表明我将面对所有类型的情况。谢谢@Gordon,但日期并不像我现在在回答中解释的那么少。但既然我每个月都想做这个分析,也许我可以写一些脚本。
select
    d.dte, count(e.start) as cnt
from 
(
select dte from
    (select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) dte from
    (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
    (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
    (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
    (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
    (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
Where dte between '2019-01-01' and '2020-12-31'
) d
inner join events e
     on e.start <= d.dte and d.dte <= e.end
group by d.dte