Overview
Find the late deliveries by computing the days between two dates with julianday(), across months and leap years.
- julianday(b) - julianday(a)
- date(d, '+3 days')
- Only works on ISO dates
Short Course
SQLite does date maths on ISO dates: date('2024-03-30', '+3 days') is
'2024-04-02', and julianday(b) - julianday(a) is the number of days
between. Trap: this only works on YYYY-MM-DD dates; '03/04/2024' is just
text. PostgreSQL writes b - a and d + INTERVAL '3 days'.
SELECT id, julianday(delivered) - julianday(ordered) AS days
FROM parcels; -- 2024-03-30 → 2024-04-02 gives 3.0
Task
Return the id and the number of days it took for every order delivered more
than 3 days after it was ordered, ordered by id. In the database below,
order 2 took 5 days across the end of March.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | ordered_onTEXT | delivered_onTEXT |
|---|---|---|
| 1 | 2024-03-01 | 2024-03-03 |
| 2 | 2024-03-28 | 2024-04-02 |
| 3 | 2024-02-27 | 2024-03-01 |
| 4 | 2024-03-10 | 2024-03-14 |
| 5 | 2024-03-20 | NULL |
Test Suites
Pending
Next
Output