要填写的预定义日期范围[重复]

Posted

技术标签:

【中文标题】要填写的预定义日期范围[重复]【英文标题】:Predefined range of date to fill [duplicate] 【发布时间】:2021-05-17 13:34:38 【问题描述】:

我想按日期计算项目的数量,但有时表格不包含带有某些日期的行,结果我有一个洞,比如:

+------------+--------+
| 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 创建了一个临时表,它可以工作:

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 |
...

【问题讨论】:

使用calendar left join datatableCOALESCE(number, 0) 或者考虑处理应用代码中数据显示的问题 感谢@Akina,我阅读了dev.mysql.com/doc/refman/8.0/en/with.html 上的“日期系列生成”部分,但在他们的示例中,它是一个 SUM,而不是一个 COUNT。如果我尝试使用 COUNT,我永远不会得到 0,最小值是 1... 您的WHERE 条件正在删除其中包含0 值的行,因为departmentCode 将是NULL。将该条件移动到 LEFT JOIN 中,而不是 LEFT JOIN geo ON dates.date = DATE(geo.ca_time) AND departmentCode = '89' 【参考方案1】:

联合所有版本

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

【讨论】:

您好,感谢您的回答,我尝试使用我的真实查询,但 GROUP BY 存在语法错误:``` WITH recursive Date_Ranges AS ( SELECT DATE(NOW() - INTERVAL 3 MONTH ) AS 日期 UNION SELECT 日期 + 间隔 1 天 FROM Date_Ranges WHERE Date , count() - 1 AS number FROM ( SELECT date FROM Date_Ranges UNION ALL SELECT DATE(ca_time ) 作为日期从地理 WHERE DATE(ca_time) >= DATE(NOW()) - INTERVAL 3 MONTH AND departmentCode = '89' AND DATE(ca_time) 查看已编辑的答案,缺少别名。【参考方案2】:

加入你的 cte 和你的桌子:

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小提琴here

【讨论】:

谢谢,但是sql结果有一些漏洞:``` +------------+--------+ |日期 |号码 | +------------+--------+ | 2021-02-21 | 1 | | 2021-02-22 | 11 | | 2021-02-27 | 8 | ``` @Surfoo ,这对我来说非常有用,请参阅 dbfiddle here

以上是关于要填写的预定义日期范围[重复]的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server:填写每个实体具有不同日期范围的缺失日期

仅通过输入日期 (jj) 填写日期 (jj/mm/yyyy)

按自定义日期范围输入过滤 wordpress 帖子

计算介于预定义日期范围之间的天数

日期范围的自定义ActiveAdmin过滤器

遍历 Pandas Dataframe 中定义的日期时间索引范围