简体   繁体   English

计算特定月份中每天的所有记录

[英]Count all records per day in a specific month

I have a table named 'incidents' in MariaDB with the data below我在 MariaDB 中有一个名为“事件”的表,其中包含以下数据

+-----+----------+---------------------+
| ID  | status   |     created_at      |
+-----+----------+---------------------+
|  1  | open     | 2018-07-03 16:15:24 |
|  2  | open     | 2018-07-03 16:15:24 |
|  3  | open     | 2018-07-05 16:15:24 |
|  4  | open     | 2018-07-08 16:15:24 |
|  5  | closed   | 2018-07-15 16:15:24 |
+-----+----------+---------------------+

I want to count all 'open' status per day in a month with a parameter to choose which month and year to get.我想计算一个月内每天的所有“打开”状态,并使用一个参数来选择要获得的月份和年份。

For example my value for month is '07' and year '2018'.例如,我的月份值为“07”,年份为“2018”。 It should return:它应该返回:

+-------+------------+
| count |    date    |
+-------+------------+
|   0   | 2018-07-01 |
|   0   | 2018-07-02 |
|   2   | 2018-07-03 |
|   0   | 2018-07-04 |
|   1   | 2018-07-05 |
|   0   | 2018-07-06 |
|   0   | 2018-07-07 |
|   1   | 2018-07-08 |
and so on up to July 31(should also be dynamic depending on month and year)
+-------+------------+

Try below query:尝试以下查询:

   select thedate,case when status is null then 0 else 1 end as count
     from (Select '2018-07-01' As [TheDate]
         Union All
         Select DateAdd(month, 1, TheDate) From dt Where [TheDate] < '2018-07-31') as dt left join tablename 
    on dt.thedate=tablename.date

Use below code to achieve your result set.使用下面的代码来实现你的结果集。

Declare Two variables:声明两个变量:

var_from_date = Date of First day of the month. var_from_date = 本月第一天的日期。 var_daydiff = Number of days of month var_daydiff = 一个月的天数

         WITH RECURSIVE cte_days (months, days, dates, n) AS
        (
          SELECT DATE(DATE_ADD(var_from_date, INTERVAL 0 DAY)) as dates,
                    0 as n
          UNION ALL
          SELECT DATE(DATE_ADD(var_from_date, INTERVAL n+1 DAY)) as dates, 
                    n + 1 as n 
          FROM cte_days WHERE n <= var_daydiff
        )
        SELECT ch.date, IFNULL(COUNT(t.id), 0) as count
        FROM cte_days ch
        LEFT JOIN table t on ch.dates = t.date
        GROUP BY ch.date;

below way you can generate date according to your requirement i have given where gen_date between '2018-07-01' and '2018-12-31' you have to use your date condition that place and use case when and aggregate function下面的方式你可以根据你的要求生成日期我已经给出where gen_date between '2018-07-01' and '2018-12-31'你必须使用你的日期条件,该地点和用case whenaggregate函数

    select gen_date as date ,sum(case when status='open' then 1 else 0 end) as Cnt from

    (    select * from 
        (select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date 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 gen_date between '2018-07-01' and '2018-12-31'
    ) as T1 left join your_table t2 on T1.gen_date= date(t2.created_at)
group by gen_date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM