-- Daily service report reconciliation -- Fictional service records for one site and one day. -- Run this SELECT in SQLite. Each completed service should appear once in each list. -- Expected exceptions: service 402 (1 confirmed, 2 reported) and 403 (1 confirmed, 0 reported). WITH confirmed_services(service_id) AS ( VALUES (401), (402), (403), (404) ), daily_report(service_id) AS ( VALUES (401), (402), (402), (404) ), entries AS ( SELECT service_id, 1 AS confirmed_rows, 0 AS reported_rows FROM confirmed_services UNION ALL SELECT service_id, 0, 1 FROM daily_report ) SELECT service_id, SUM(confirmed_rows) AS confirmed, SUM(reported_rows) AS reported FROM entries GROUP BY service_id HAVING SUM(confirmed_rows) <> 1 OR SUM(reported_rows) <> 1 ORDER BY service_id;