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.

On this page
  1. What an invoice line records
  2. The monthly workflow
  3. Loading each line once
  4. Less liquor, at a higher price per liter
  5. Who buys: gas stations took share from supermarkets
  6. The board report: FY2025 against FY2024
  7. Explore the books yourself
  8. Reusing the workflow

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

Revenue by Iowa fiscal year (July to June), in millions of dollars, from every row of every file and from the ledger, where each line appears once. The two points are the totals in the state's FY2025 annual report: $444.9 million for FY2024, against $446.0 million in the ledger. FY2012 covers January to June 2012 only, so it is left off.SourceIowa Data Hub, Iowa Liquor Sales; Iowa Department of Revenue, FY2025 Annual Report

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

  1. Read each zip in place, one CSV part at a time

    The zipfs extension lets read_csv open 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

  2. Merge each part into the ledger

    MERGE INTO inserts 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

  3. 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

  4. Build the year-over-year report with subtotals

    GROUP BY ROLLUP adds the grand total row to the family rows in the same query, and grouping() 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);

    10 rowschapters/12-liquor-wholesale/pipeline/20_model.sql

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

Cumulative contribution to the change in yearly revenue since 2012, chained one year at a time, in millions of dollars. Revenue went from $255.2 million in 2012 to $424.4 million in 2025. Volume added $41.2 million, price changes on the same items $55.2 million and the move toward pricier items $72.9 million. Volume's cumulative contribution has shrunk since liters peaked in 2021. Revenue is what stores paid the state, not shelf prices.SourceIowa Data Hub, Iowa Liquor Sales 2012 to 2025, read 3 December 2026
Querychapters/12-liquor-wholesale/pipeline/20_model.sqlShow SQL
20_model.sqlSQL
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

Share of each year's revenue, by spirit family, in percent. Tequila and mezcal went from 5.4% in 2012 to 10.4% in 2025, rum from 14.2% to 8.0%. Families come from the first three digits of the category code; Fireball and other flavored whiskies count as liqueurs, as the state files them.SourceIowa Data Hub, Iowa Liquor Sales 2012 to 2025

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

$0.00$10.00$20.00$30.00$40.00Dollars per liter, 2025Tequila and mezcal: $33.12 (2012 to 2025 $19.55 to $33.12)Tequila and mezcal$33.12Brandy: $24.48 (2012 to 2025 $16.36 to $24.48)Brandy$24.48Liqueurs and schnapps: $24.02 (2012 to 2025 $16.14 to $24.02)Liqueurs and schnapps$24.02Whiskey: $23.85 (2012 to 2025 $15.13 to $23.85)Whiskey$23.85Gin: $19.42 (2012 to 2025 $12.44 to $19.42)Gin$19.42Cocktails and gift packs: $18.20 (2012 to 2025 $8.88 to $18.20)Cocktails and gift packs$18.20Rum: $15.25 (2012 to 2025 $13.24 to $15.25)Rum$15.25Vodka: $12.73 (2012 to 2025 $10.89 to $12.73)Vodka$12.73
Iowa spirits revenue per liter by family in 2025, with the 2012 value
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
Revenue per liter by spirit family; the small Other group is not shown. Bars are 2025; the interval runs from the 2012 value to the 2025 value. Within a family the rise mixes price changes with moves to pricier items. Not adjusted for inflation.SourceIowa Data Hub, Iowa Liquor Sales 2012 and 2025

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%

Share of each year's revenue by chain, in percent, with each store assigned to a chain by its latest name. The remaining 43% in 2025 goes to independent liquor stores, smaller chains and other grocers. Chains that renamed their stores (Kum & Go became Maverik) are not separated.SourceIowa Data Hub, Iowa Liquor Sales 2012 to 2025

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 spirits446M435.5M-2.4%-2.8%
Whiskey145.5M139M-4.5%-5.2%
Vodka94.6M92.8M-1.9%-1.2%
Liqueurs and schnapps67.7M66.9M-1.2%-4.1%
Tequila and mezcal45.7M45.5M-0.5%-2.6%
Rum37.7M35.4M-6.2%-5.2%
Cocktails and gift packs20.2M25M24.0%11.8%
Brandy15.1M13.1M-13.3%-8.1%
Gin9.6M9.3M-3.2%-5.0%
Other9.7M8.4M-13.7%-4.9%
10 rows
Revenue and liters by spirit family, Iowa fiscal years 2024 and 2025 (July to June), with the ROLLUP total as the All spirits row. Click a header to sort.SourceIowa Data Hub, Iowa Liquor Sales, July 2023 to June 2025

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 family
  • storesEvery store: name, chain, city, county
  • vendor_monthSales by month, vendor and spirit family
  • items_yearSales by year and product
  • store_yearSales by year, store and spirit family
  • chain_monthSales by month, spirit family and chain
  • county_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

Shell
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 nothing

When 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.