SQL • Reporting accuracy
SQL Date Filters: Stop Double-Counting Midnight Orders
Test adjacent daily reports with a six-row fixture before trusting a revenue dashboard.
How do you avoid counting an order in two reporting periods?
Use an inclusive start and an exclusive end: ordered_at >= start_time AND ordered_at < next_start_time. Reuse the same boundary as the next period’s start. An order exactly at midnight then belongs to one period. BETWEEN includes both endpoints, so consecutive reports that share an endpoint can both count the same order.
The PostgreSQL comparison reference confirms that BETWEEN is equivalent to a greater-than-or-equal lower bound and a less-than-or-equal upper bound. The issue is the reporting interval you choose, not a malfunction in BETWEEN.
A revenue fixture with deliberately awkward times
Imagine a marketing agency reconciling a client’s daily order dashboard. These fictional orders are already expressed in integer USD cents. The reporting timezone has been agreed upstream. Order 2 occurs exactly at the start of October 1; order 4 occurs in its final fractional second; order 5 starts October 2. Order 6 has no timestamp.
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
ordered_at TEXT,
amount_cents INTEGER NOT NULL
);
INSERT INTO orders VALUES
(1,'2026-09-30 23:59:59.999999',1000),
(2,'2026-10-01 00:00:00.000000',2000),
(3,'2026-10-01 12:00:00.000000',3000),
(4,'2026-10-01 23:59:59.999999',4000),
(5,'2026-10-02 00:00:00.000000',5000),
(6,NULL,6000);Why BETWEEN includes tomorrow’s midnight
SELECT order_id, amount_cents
FROM orders
WHERE ordered_at BETWEEN '2026-10-01 00:00:00.000000'
AND '2026-10-02 00:00:00.000000'
ORDER BY order_id;This returns orders 2, 3, 4, and 5: 14,000 cents, or $140. October 1 should contain only orders 2–4, totaling $90. The upper endpoint incorrectly contributes October 2’s $50 order to this daily report.
If October 2’s query starts at that same midnight and also uses BETWEEN, order 5 appears again. A dashboard can show plausible daily values while its sum of daily totals exceeds the total for the combined period.
Use a half-open interval
SELECT order_id, amount_cents
FROM orders
WHERE ordered_at >= '2026-10-01 00:00:00.000000'
AND ordered_at < '2026-10-02 00:00:00.000000'
ORDER BY order_id;The corrected result is orders 2, 3, and 4, totaling 9,000 cents ($90). It includes the exact start, retains the final fractional second, and excludes the next day’s start.
Why not end at 23:59:59?
An inclusive upper bound of 2026-10-01 23:59:59.000000 excludes order 4, which occurs later in the same second. That report returns only $50 instead of $90. Adding an assumed number of nines couples the query to timestamp precision. The exclusive next-period boundary avoids that dependency.
Prove that adjacent daily totals reconcile
Use September 30 midnight through October 1 midnight for the first interval, and October 1 midnight through October 2 midnight for the second. Both intervals use the same inclusive-start/exclusive-end convention.
SELECT COUNT(*) AS order_count, SUM(amount_cents) AS revenue_cents
FROM orders
WHERE ordered_at >= '2026-09-30 00:00:00.000000'
AND ordered_at < '2026-10-02 00:00:00.000000';The combined interval contains four orders and 10,000 cents ($100). September 30 contributes order 1 and $10; October 1 contributes three orders and $90. The daily ID sets have no overlap, and $10 + $90 = $100. Check both properties: matching aggregate totals alone could conceal offsetting errors.
Do not silently lose orders with unknown times
SELECT COUNT(*) AS missing_time_orders,
SUM(amount_cents) AS missing_time_revenue_cents
FROM orders
WHERE ordered_at IS NULL;The audit finds one order worth 6,000 cents ($60). A comparison with a missing timestamp does not pass WHERE. Keep that amount in a data-quality exception report until its date is established; assigning it to today would invent a business fact. Order 5 is outside the combined period, while order 6 is unallocatable. Those are different reasons for exclusion.
Apply the rule to US, UK, and European reporting
First agree whether a day means UTC or a named business timezone. If storage is UTC but the report follows New York, London, or Berlin, derive both local midnight boundaries and convert them to the corresponding storage instants. On daylight-saving transitions, consecutive local midnights need not be 24 elapsed hours apart. This fixture tests interval inclusion only; it does not perform timezone conversion.
For a month, use the first instant of the next month as the exclusive end. For a genuinely date-only column, BETWEEN two calendar dates can be appropriate because there is no time-of-day fraction to include. Keep date and timestamp requirements explicit.
Boundary checks to include in a report review
- A record immediately before the start must be excluded.
- A record exactly at the start must be included.
- A fractional-second record just before the end must be included.
- A record exactly at the end must be excluded and belong to the next interval.
- Missing timestamps must have a separately visible count and amount.
- Adjacent intervals must have disjoint IDs and reconcile to the combined interval.
Validation: six assertions passed in Python’s SQLite engine for the inclusive-end failure, corrected IDs, fractional-second loss, disjoint daily sets, combined revenue, and missing-time audit. This is a reproducible synthetic correctness exercise, not a database performance benchmark or a claim about a client’s results.
Continue learning
Review SQL dates and reporting boundaries for PostgreSQL timestamp syntax and monthly grouping. Use WHERE and missing-value examples to understand filter behavior, and NULL versus zero in revenue to distinguish missing amounts from real zero-value orders.