How can days with no observations still appear in a report?
A recursive CTE can build a bounded date spine. Left joining events onto that spine preserves empty days.
SQLite example and interpretation
Only day two has two events in a three-day report. Count a matched right-hand field because counting rows would report one on empty days.
WITH RECURSIVE d(t) AS (VALUES('2024-01-01') UNION ALL SELECT date(t,'+1 day') FROM d WHERE t<'2024-01-03'), e(t) AS (VALUES('2024-01-02'),('2024-01-02')) SELECT d.t,count(e.t) FROM d LEFT JOIN e ON d.t=e.t GROUP BY d.t ORDER BY d.t;Result [["2024-01-01", 0], ["2024-01-02", 2], ["2024-01-03", 0]]
中文
缺少数据的日期如何也出现在日报中?
递归 CTE 可从起始日期生成有限日历,再以日历为左表关联事件。
SQLite 示例与适用条件
三天内只有第二天有两条事件。COUNT 应数匹配的右侧字段,直接数行会给空日错误地算一条。
WITH RECURSIVE d(t) AS (VALUES('2024-01-01') UNION ALL SELECT date(t,'+1 day') FROM d WHERE t<'2024-01-03'), e(t) AS (VALUES('2024-01-02'),('2024-01-02')) SELECT d.t,count(e.t) FROM d LEFT JOIN e ON d.t=e.t GROUP BY d.t ORDER BY d.t;结果 [["2024-01-01", 0], ["2024-01-02", 2], ["2024-01-03", 0]]