Part IV — Business workflows · Chapter 10
A state wholesaler's books, rebuilt from its invoices
Iowa is the only wholesaler of spirits in the state, and it publishes every invoice line since 2012. Loading the files naively overstates recent revenue by a fifth; loaded idempotently, they match the state's annual report, and they show sales growing on mix and price while volume fell after 2021.
Load every row of Iowa's published liquor invoices for the year to June 2025 and they add up to $523.6 million. The state's annual report says it sold $434.9 million; the files overstate it by 20%. The gap is a quirk of how the files are exported: in every year from 2020 to 2025, the last CSV part repeats rows of the part before it. Load each invoice line once and the same files give $435.5 million, within 0.1% of the report, and the state's largest customer comes out the same to the dollar.
This chapter does what an analyst at a distributor does each month. It loads the new invoices without double-counting, maps a product hierarchy that was renamed partway through, builds the year-over-year report with subtotals, and splits revenue growth into volume, price and mix. Iowa is a "control state": its Alcoholic Beverages Division is the only wholesaler of spirits, so every bottle a grocery store, liquor store or gas station buys is an invoice line in this dataset.
Counting every row of every file overstates revenue by about a fifth from fiscal 2021
What an invoice line records
Each line carries the invoice, date, store (number, name, address, county), product (item number, description, category, vendor, bottle size, pack) and the sale (bottles, dollars, liters). The dollar figure is the state's price to the licensee, which includes the state's markup; stores set their own shelf prices. Iowa moved its data portal to a new Data Hub in 2026, and the old Socrata API that most tutorials use no longer answers.
The monthly workflow
Read each zip in place, one CSV part at a time
The
zipfsextension letsread_csvopen a member of a zip archive by path, so nothing is unpacked to disk. Every column is read as text, and the line's content key is the MD5 of the whole row.00_sources.sqlSQL CREATE OR REPLACE MACRO part_file(path) AS TABLE SELECT md5_number(concat_ws('|', *COLUMNS(*))) AS line_key, * FROM read_csv(path, all_varchar = true, header = true);66 partschapters/12-liquor-wholesale/pipeline/00_sources.sql
Merge each part into the ledger
MERGE INTOinserts only lines whose key is not in the ledger yet. Loading a part twice inserts nothing, so the current year's file can be reloaded every month as the state adds to it, and a repeated part does no harm.10_load.sqlSQL MERGE INTO ledger AS l USING incoming_distinct AS s ON l.line_key = s.line_key WHEN NOT MATCHED THEN INSERT VALUES (s.line_key, s.invoice_id, s.ordered_on::DATE, …);35,501,563 lines inserted, 3,136,051 already loaded; 106 schapters/12-liquor-wholesale/pipeline/10_load.sql
Conform the category hierarchy
The category names were rewritten in 2016 (TEQUILA split into 100% AGAVE TEQUILA and MIXTO TEQUILA; VODKA 80 PROOF became AMERICAN VODKAS), but the first three digits of the seven-digit category code mark the same spirit throughout. Lines with no code (17,000) take their item's most common code; 14,675 of them get one.
20_model.sqlSQL SELECT l.*, coalesce(f.family, 'Other') AS family FROM ledger l LEFT JOIN item_code i USING (item_no) LEFT JOIN family_map f ON f.prefix = coalesce(l.category_code, i.item_category_code) // 10000;107 category names mapped to 9 familieschapters/12-liquor-wholesale/pipeline/20_model.sql
Build the year-over-year report with subtotals
GROUP BY ROLLUPadds the grand total row to the family rows in the same query, andgrouping()marks which row is which.20_model.sqlSQL SELECT coalesce(family, 'All spirits') AS family, grouping(family) AS is_total, sum(dollars) FILTER (WHERE fy = 2024) AS dollars_fy24, sum(dollars) FILTER (WHERE fy = 2025) AS dollars_fy25 FROM lines WHERE fy IN (2024, 2025) GROUP BY ROLLUP (family);
Loading each line once
The load log shows the problem without any analysis. For 2012 to 2019 every row inserts. For 2020 to 2025, the fifth part of each file inserts nothing: its 574,597 rows in 2020 and 477,405 in 2025 are all already in the ledger from the fourth part. Across the 6 files, 3,136,051 rows are repeats. A few lines also repeat inside a single part (31 in all, in 2025 and 2026); with no line key, the content key counts each such pair once, which may drop a few genuine second orders.
The state's FY2025 report gives four more values to check. Its largest customer, Hy-Vee #3 in Des Moines, bought $13,979,923, and the ledger has $13,979,923. The top 50 customers come within $174 of the report's $140.4 million, and Polk County within 0.4%. The report's fiscal year includes a hold-open period into September, which explains why the totals agree closely but not exactly. checks.sql fails the run if any of these drifts by more than 0.5%.
Less liquor, at a higher price per liter
Iowa stores bought 24.8 million liters in 2021 and 22.1 million in 2025, 11% less. Revenue fell less, from $446.1 million in 2023 to $424.8 million in 2025, because the average liter cost more: $13.61 in 2012 and $19.26 in 2025.
Spirits revenue grew by $169 million from 2012 to 2025, and more of the growth came from mix than from price or volume
Querychapters/12-liquor-wholesale/pipeline/20_model.sqlShow SQL
SELECT b.year,
(b.q - a.q) * a.r / a.q AS volume, -- liters at last year's average price
p.price, -- same item, new price
b.r - a.r - (b.q - a.q) * a.r / a.q - p.price AS mix -- the rest: which items
FROM tot b JOIN tot a ON a.year = b.year - 1 JOIN price p ON p.year = b.year;13 year-to-year stepsRuns at build time in DuckDB 2.0; shown for reading.Open file
The bridge above separates the two sources of that rise, and splits the whole change in revenue three ways: volume, price on the same item, and mix. Price is the change in price of the same item (same product and bottle size) from one year to the next; it added $55.2 million over 13 years. Mix is the shift toward items that cost more per liter, including new ones; it added $72.9 million. Mix was largest in 2021, at $21.2 million, the year after the pandemic's volume jump of $29.9 million. In 2025 mix turned negative ($-4.7 million) as volume fell $24.0 million.
Tequila's share of spirits revenue nearly doubled; rum's fell by almost half
Tequila is also the most expensive family per liter, at $33.12 in 2025, so its gain moves the average up. Vodka is the cheapest, at $12.73, and its share of liters rose from 29% to 32% while its share of dollars slipped. Whiskey's price per liter rose from $15.13 to $23.85.
Every named family costs more per liter than in 2012; tequila rose most
| Tequila and mezcal | $33.12 | $19.55 to $33.12 |
|---|---|---|
| Brandy | $24.48 | $16.36 to $24.48 |
| Liqueurs and schnapps | $24.02 | $16.14 to $24.02 |
| Whiskey | $23.85 | $15.13 to $23.85 |
| Gin | $19.42 | $12.44 to $19.42 |
| Cocktails and gift packs | $18.20 | $8.88 to $18.20 |
| Rum | $15.25 | $13.24 to $15.25 |
| Vodka | $12.73 | $10.89 to $12.73 |
Who buys: gas stations took share from supermarkets
The number of stores buying from the state went from 1,226 in 2012 to 2,243 in 2025. Many of the new ones are convenience stores. Casey's, the Iowa gas-station chain, bought 0.9% of the state's spirits in 2012 and 8.1% in 2025, from 551 stores. Hy-Vee, the largest grocery chain, went from 36.2% to 30.3%.
Casey's went from 1% of the state's spirits revenue to 8%; Hy-Vee fell from 36% to 30%
The board report: FY2025 against FY2024
The table is the report a distributor sends its board each year: revenue and liters by family for the last two fiscal years, with a total. In FY2025 revenue fell 2.4% and liters 2.8%. The one family that grew was cocktails and gift packs, up 24%, which includes ready-to-drink cans.
Only cocktails and gift packs grew in fiscal 2025
| All spirits | 446M | 435.5M | -2.4% | -2.8% |
| Whiskey | 145.5M | 139M | -4.5% | -5.2% |
| Vodka | 94.6M | 92.8M | -1.9% | -1.2% |
| Liqueurs and schnapps | 67.7M | 66.9M | -1.2% | -4.1% |
| Tequila and mezcal | 45.7M | 45.5M | -0.5% | -2.6% |
| Rum | 37.7M | 35.4M | -6.2% | -5.2% |
| Cocktails and gift packs | 20.2M | 25M | 24.0% | 11.8% |
| Brandy | 15.1M | 13.1M | -13.3% | -8.1% |
| Gin | 9.6M | 9.3M | -3.2% | -5.0% |
| Other | 9.7M | 8.4M | -13.7% | -4.9% |
Explore the books yourself
The dashboard below runs the chapter's data in your browser. The pipeline also writes seven Parquet extracts (monthly sales by store and spirit, the store list, vendors by month, products by year, and three small rollups the panels read: by year and store, by month and chain, by county and month) to public storage, and DuckDB-Wasm reads them over HTTP range requests, so a filter fetches only the row groups and columns it needs. Every panel shows its SQL. The Ask box at the bottom hands your question to an AI model that writes the SQL; the query runs here, on the same files, and the model only sees the small result. Its answers are live and unaudited, unlike the numbers above.
Live · runs in your browser
Iowa liquor sales, 2012 to November 2026
- Engine
- DuckDB 2.0.1 · Wasm
- Data read
- 0 B of 26 MB
The chapter's data sits in 7 Parquet files on public storage, 26 MB in all. Start the engine and DuckDB runs inside this page: each chart below is a SQL query that reads only the row groups and columns it needs over HTTP range requests.
monthlySales by month, store and spirit familystoresEvery store: name, chain, city, countyvendor_monthSales by month, vendor and spirit familyitems_yearSales by year and productstore_yearSales by year, store and spirit familychain_monthSales by month, spirit family and chaincounty_monthSales by county, month, spirit family and chain
Queries this chapter's published snapshot; the data does not update in real time. No account, nothing uploaded.
Reusing the workflow
DOWNLOAD=1 bash chapters/12-liquor-wholesale/pipeline/run.sh # 15 zips, then load, model, export, checks
RELOAD=1 bash chapters/12-liquor-wholesale/pipeline/run.sh # reload into the same ledger: inserts nothingWhen a source publishes snapshots instead of changes, make the load idempotent: give each line a key, merge instead of append, and log what each file inserted and what it repeated. The log finds export problems before any analysis does, and a known total from the publisher's own report tells you which version of the numbers is right. Full pipeline: chapters/12-liquor-wholesale.