December 23, 2026 · Advent of Postgres
23. Setting it right Solved
MediumSQL functionFILTER
Tansy is ready to publish the corrected Nice List. Before she does, she wants the same count you gave her on day 5, done properly.
Your task
Count the children on each side of the true list, ignoring the fake entries (night entries on LOGI-07). Return status ('nice' or 'naughty') and children, sorted by status.
Your answer returns 2 rows with the columns status, children. Order matters.
What happens next
Six naughty children, just like last year. Tansy laughs so hard her hat falls off. Holly, Juniper and Sprig, back from the spa and very relaxed, are horrified to hear what happened to their badges, and Grizzle has offered to wrap every one of the thirty children's presents by hand.
Solve today's puzzle to read what happens next.
The data
The North Pole's tables, a snapshot taken on December 1, 2026. Times are North Pole time (UTC). PostgreSQL runs in your browser: use Explore data to browse it.
elvesid, name, department, hired_on DATE, manager_id (→ elves.id, NULL for the Head Elf)elf_leaveelf_id, from_date DATE, to_date DATE (both days included)childrenid, name, country, city, utc_offset DOUBLE PRECISION, agewisheschild_id, toy_id, rank (1 = top wish)toysid, name, category, build_minutes, weight_gnice_list_logid, child_id, logged_at TIMESTAMP, points, reason, recorded_by (→ elves.id), terminal (→ terminals.id)terminalsid, location, departmentreindeerid, name, max_load_kgflightsid, reindeer_id, flown_on DATE, km, carrotsbuildsid, toy_id, elf_id, built_at TIMESTAMP, minutes, passed_qc BOOLEANcookie_batchesid, baked_at TIMESTAMP, elf_id, cookiesdoor_swipesid, elf_id, swiped_at TIMESTAMP, door, direction ('in' or 'out')
Hints
Solution
CREATE OR REPLACE FUNCTION is_fake(terminal text, at timestamp) RETURNS boolean
LANGUAGE sql IMMUTABLE
RETURN terminal = 'LOGI-07' AND at::time < '05:00';
SELECT CASE WHEN score >= 0 THEN 'nice' ELSE 'naughty' END AS status,
count(*) AS children
FROM (
SELECT child_id, coalesce(sum(points) FILTER (WHERE NOT is_fake(terminal, logged_at)), 0) AS score
FROM nice_list_log
GROUP BY child_id
) AS s
GROUP BY status
ORDER BY status;
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Today's puzzle is also on Advent of SQL, Advent of DuckDB, Advent of MongoDB.