要填写的预定义日期范围

Predefined range of date to fill

我想按日期计算项目的数量,但有时 table 不包含带有某些日期的行,结果出现漏洞,例如:

+------------+--------+
| date       | number |
+------------+--------+
| 2021-03-12 |      1 |
| 2021-03-15 |      1 |
| 2021-03-17 |      1 |
| 2021-03-18 |      1 |
+------------+--------+

我想要这个结果:

+------------+--------+
| date       | number |
+------------+--------+
| 2021-03-12 |      1 |
| 2021-03-13 |      0 |
| 2021-03-14 |      0 |
| 2021-03-15 |      1 |
| 2021-03-16 |      0 |
| 2021-03-17 |      1 |
| 2021-03-18 |      1 |
+------------+--------+

所以,我用 WITH 创建了一个临时 table,它有效:

WITH recursive Date_Ranges AS (
       select DATE(NOW() - INTERVAL 3 MONTH) AS Date
       union
       select Date + interval 1 day
       from Date_Ranges
       where Date < DATE(NOW())
   )
    SELECT *, 0 AS number FROM Date_Ranges

接下来,我想检索统计信息,我尝试了这个查询:

WITH recursive Date_Ranges AS (
   select DATE(NOW()- INTERVAL 3 MONTH) AS Date
   union
   select Date + interval 1 day
   from Date_Ranges
   where Date < DATE(NOW())
)
select *, '0' AS value from Date_Ranges
UNION DISTINCT 
SELECT DATE(time) AS date, count(*) AS number
                    FROM my_table
                    WHERE time >= NOW()- INTERVAL 3 MONTH
                    AND time <= DATE(NOW())
                    GROUP BY DATE(ca_time)

但是 UNION 不起作用,因为值不同所以 mysql 保留 2 行:

example:
+------------+--------+
| date       | number |
+------------+--------+
| 2021-03-12 |      0 |
| 2021-03-13 |      0 |
| 2021-03-14 |      0 |
| 2021-03-15 |      0 |
| 2021-03-16 |      0 |
| 2021-03-17 |      0 |
| 2021-03-18 |      0 |
| 2021-03-12 |      2 |
| 2021-03-13 |      6 |
| 2021-03-14 |      7 |
| 2021-03-15 |      8 |
| 2021-03-16 |      9 |
| 2021-03-17 |      5 |
| 2021-03-18 |      0 |
+------------+--------+

如何将带有 0 的行与第二个并集合并?

谢谢。

编辑:

我试过这个查询,但没有结果的日期不在这里:

WITH recursive dates AS (
   SELECT DATE(NOW() - INTERVAL 3 MONTH) AS Date
   UNION ALL
   SELECT Date + interval 1 day
   FROM dates
   WHERE Date < DATE(NOW())
)
SELECT date, COALESCE(COUNT(1), 0) AS number
FROM dates
LEFT JOIN geo
ON dates.date = DATE(geo.ca_time)
WHERE departmentCode = '89'
GROUP BY geo.ca_time
ORDER BY geo.ca_time

结果:

+------------+--------+
| date       | number |
+------------+--------+
| 2021-02-21 |      1 |
| 2021-02-22 |     11 |
| 2021-02-27 |      8 |
...

加入你的 cte 和你的 table :

WITH recursive Date_Ranges AS (
   select DATE(NOW()- INTERVAL 3 MONTH) AS Date
   union
   select Date + interval 1 day
   from Date_Ranges
   where Date < DATE(NOW())
)

select dr.date ,coalesce(sum(t.num),0)
from Date_Ranges dr
left join my_table t
 on dr.date = t.datecol
group by dr.date

db<>fiddle here

联合所有版本

WITH recursive Date_Ranges AS (
   select DATE(NOW()- INTERVAL 3 MONTH) AS Date
   union
   select Date + interval 1 day
   from Date_Ranges
   where Date < DATE(NOW())
)   
select date, count(*) - 1 AS number
from (
   select date
   from Date_Ranges
   union all
   select DATE(time) 
   from my_table
   where time >= NOW() - INTERVAL 3 MONTH
             AND time <= DATE(NOW())
) t
group by date

db<>fiddle