# THE STOCKTAKE

WEEKLY QUACK CHALLENGE! · challenge 20 · medium · 5 guesses

> Quartermaster Mandarin drops a ledger and a stack of count sheets on the desk, ink still wet. "Winter stocktake, Duckbert. The ledger says what every storeroom should hold, and the counters wrote down what is actually on the shelves. The Tackle Room came up short on hooks, bobbers and bait. The Honey Vault can't find sixty beeswax candles. The Net Locker is full of cork floats nobody ever booked. And the Boathouse counted its life rings one shelf at a time and now swears five in six of them are gone! I can audit one storeroom before the pond freezes. Find the one where the books and the shelves disagree the most!"

## Task

Return the name of the storeroom with the largest total discrepancy. `ledger` holds the quantity each storeroom should have of each item. `shelf_counts` holds what the counters found; an item kept on several shelves has several rows, and its count is their total. An item's discrepancy is the difference between its ledger quantity and its count, as a positive number whichever side is larger; an item missing from either table counts as zero there. The same item name can appear in more than one storeroom, and each storeroom's stock is separate. A storeroom's total is the sum of its items' discrepancies. On a tie, return the alphabetically first storeroom name. You will be working with the `ledger` and `shelf_counts` tables.

## Tables

### `ledger` (25 rows)

| column | type |
| --- | --- |
| storeroom | VARCHAR |
| item | VARCHAR |
| on_books | INTEGER |

### `shelf_counts` (39 rows)

| column | type |
| --- | --- |
| count_id | INTEGER |
| storeroom | VARCHAR |
| item | VARCHAR |
| shelf | VARCHAR |
| counted | INTEGER |

## Data

The tables live in a DuckDB database: https://dbquacks.com/data/databases/weekly_202647_20_stocktake.duckdb

To query it outside the browser, attach it read-only:

```sql
ATTACH 'https://dbquacks.com/data/databases/weekly_202647_20_stocktake.duckdb' AS db (READ_ONLY);
USE db;
```

Play it in the browser: https://dbquacks.com/challenges/weekly/20

## Answer

The expected answer is not published. Submit on the page to have it checked.
