Calendar reporting
Overview
A calendar report groups events into meaningful time periods. Define the reporting zone, period boundaries and whether periods with no events should appear before writing the query.
First example
SQLite 3.46.1. Load calendar-fixture.sql first.
SELECT strftime('%Y-%m',occurred_utc) AS month, COUNT(*) AS events FROM calendar_events WHERE occurred_utc >= '2025-01-01 00:00:00' AND occurred_utc < '2025-04-01 00:00:00' GROUP BY strftime('%Y-%m',occurred_utc) ORDER BY month;Observed rows (JSON notation):
[ [ "2025-01", 2 ], [ "2025-03", 1 ]]How it works
Use a half-open range: start <= timestamp and timestamp < end. The fixed UTC text fixture compares chronologically because every stored value has the same sortable format and zone. A grouped event query omits empty months. To include them, begin with a calendar table and LEFT JOIN matching events, then COUNT a non-null event identifier rather than COUNT(*). Joins and aggregates are previews of later chapters; the result can be understood as counting matches for each month.
Practical example
SQLite 3.46.1. Load calendar-fixture.sql first.
SELECT substr(m.start_utc,1,7) AS month, COUNT(e.id) AS eventsFROM calendar_months AS m LEFT JOIN calendar_events AS eON e.occurred_utc >= m.start_utc AND e.occurred_utc < m.end_utcGROUP BY m.start_utc ORDER BY m.start_utc;Observed rows (JSON notation):
[ [ "2025-01", 2 ], [ "2025-02", 0 ], [ "2025-03", 1 ]]Practice and common mistakes
Do not use the last visible second of a month as an inclusive boundary: fractional seconds may be missed. Do not move an event-side filter into WHERE if it removes unmatched calendar rows. Practice: add an April period and predict its event count (1). The fixed fixture contains a December event outside the report and an April event on its exclusive end.