{
 "about": "40 questions from an independent author who never saw the documentation, the marts page or the earlier questions. gold_sql is the answer key, written from the question text under the documented definitions before the author's own SQL was read. All values are integers (money in whole cents) or text labels.",
 "selection": "The author wrote 44. Ten keys came back empty. The last four empty-key questions in the author's order were dropped before any model saw the set; the other six stay and are flagged empty_key.",
 "dropped_author_order": [
  38,
  39,
  42,
  43
 ],
 "empty_key_ids": [
  "P27",
  "P30",
  "P33",
  "P34",
  "P36",
  "P37"
 ],
 "questions": [
  {
   "id": "P01",
   "author_order": 1,
   "kind": "analytical",
   "text": "What was total CAD card purchasing during July 2026? Return one column: purchases in whole CAD cents.",
   "columns": [
    "purchases_cents"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(purchased_minor),0) FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     1342646
    ]
   ],
   "author_reading": "Sum the recorded purchase amounts in the daily card spending records dated July 1 through July 31, restricted to CAD. Return zero if there are no matching records.",
   "author_sql": "SELECT COALESCE(SUM(purchased_minor), 0) AS purchases_cents FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'"
  },
  {
   "id": "P02",
   "author_order": 2,
   "kind": "analytical",
   "text": "How much was refunded on USD cards during July 2026? Return one column: refunds in whole USD cents.",
   "columns": [
    "refunds_cents"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(refunded_minor),0) FROM card_spend_daily WHERE currency='USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     26227
    ]
   ],
   "author_reading": "Sum the recorded refund amounts in July's USD daily card spending records. Return zero when there are no matching records.",
   "author_sql": "SELECT COALESCE(SUM(refunded_minor), 0) AS refunds_cents FROM card_spend_daily WHERE currency = 'USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'"
  },
  {
   "id": "P03",
   "author_order": 3,
   "kind": "analytical",
   "text": "What was net CAD card spending during July 2026? Return one column: net spending in whole CAD cents.",
   "columns": [
    "net_spend_cents"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(net_spend_minor),0) FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     1277992
    ]
   ],
   "author_reading": "Use the recorded net spending amounts in July's CAD daily card records. Return zero if none exist.",
   "author_sql": "SELECT COALESCE(SUM(net_spend_minor), 0) AS net_spend_cents FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'"
  },
  {
   "id": "P04",
   "author_order": 4,
   "kind": "analytical",
   "text": "How many USD card purchases were recorded during July 2026? Return one column: purchase count.",
   "columns": [
    "purchase_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(purchase_count),0) FROM card_spend_daily WHERE currency='USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     129
    ]
   ],
   "author_reading": "Add the purchase counts from July's USD daily card spending records. Count purchases rather than daily summary rows.",
   "author_sql": "SELECT COALESCE(SUM(purchase_count), 0) AS purchase_count FROM card_spend_daily WHERE currency = 'USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'"
  },
  {
   "id": "P05",
   "author_order": 5,
   "kind": "analytical",
   "text": "How many CAD card refunds were recorded during July 2026? Return one column: refund count.",
   "columns": [
    "refund_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(refund_count),0) FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     39
    ]
   ],
   "author_reading": "Add the refund counts from July's CAD daily card spending records. Return zero if there are no matching records.",
   "author_sql": "SELECT COALESCE(SUM(refund_count), 0) AS refund_count FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'"
  },
  {
   "id": "P06",
   "author_order": 6,
   "kind": "analytical",
   "text": "How many distinct cards made at least one CAD purchase during July 2026? Return one column: card count.",
   "columns": [
    "card_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COUNT(DISTINCT card_id) FROM card_spend_daily WHERE currency='CAD' AND purchase_count>0 AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     22
    ]
   ],
   "author_reading": "Count distinct cards with a positive recorded purchase count on at least one July CAD daily spending record.",
   "author_sql": "SELECT COUNT(DISTINCT card_id) AS card_count FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' AND purchase_count > 0"
  },
  {
   "id": "P07",
   "author_order": 7,
   "kind": "analytical",
   "text": "How many distinct USD accounts received a positive amount of posted money in on at least one day during July 2026? Return one column: account count.",
   "columns": [
    "account_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COUNT(DISTINCT account_id) FROM account_daily WHERE currency='USD' AND posted_credit_minor>0 AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     5
    ]
   ],
   "author_reading": "Count distinct accounts with at least one July USD daily balance record showing positive posted credits. Use the daily ledger's credit totals to define money in.",
   "author_sql": "SELECT COUNT(DISTINCT account_id) AS account_count FROM account_daily WHERE currency = 'USD' AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' AND posted_credit_minor > 0"
  },
  {
   "id": "P08",
   "author_order": 8,
   "kind": "analytical",
   "text": "What was the combined closing balance of all CAD accounts with a balance record for July 31, 2026? Return one column: closing balance in whole CAD cents.",
   "columns": [
    "closing_balance_cents"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(closing_minor),0) FROM account_daily WHERE currency='CAD' AND balance_date=DATE '2026-07-31'",
   "empty_key": false,
   "key_rows": [
    [
     125676
    ]
   ],
   "author_reading": "Sum CAD closing balances recorded for July 31. Accounts without a record for that date do not contribute.",
   "author_sql": "SELECT COALESCE(SUM(closing_minor), 0) AS closing_balance_cents FROM account_daily WHERE currency = 'CAD' AND balance_date = DATE '2026-07-31'"
  },
  {
   "id": "P09",
   "author_order": 9,
   "kind": "analytical",
   "text": "What was the combined opening balance of all USD accounts with a balance record for July 1, 2026? Return one column: opening balance in whole USD cents.",
   "columns": [
    "opening_balance_cents"
   ],
   "ordered": false,
   "gold_sql": "SELECT COALESCE(SUM(opening_minor),0) FROM account_daily WHERE currency='USD' AND balance_date=DATE '2026-07-01'",
   "empty_key": false,
   "key_rows": [
    [
     258815
    ]
   ],
   "author_reading": "Sum the opening balances in USD daily balance records for July 1. Return zero if no records match.",
   "author_sql": "SELECT COALESCE(SUM(opening_minor), 0) AS opening_balance_cents FROM account_daily WHERE currency = 'USD' AND balance_date = DATE '2026-07-01'"
  },
  {
   "id": "P10",
   "author_order": 10,
   "kind": "analytical",
   "text": "How many USD account-day balance records ended below zero during July 2026? Return one column: account-day count.",
   "columns": [
    "account_day_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COUNT(*) FROM account_daily WHERE currency='USD' AND closing_minor<0 AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     10
    ]
   ],
   "author_reading": "Count July USD daily balance rows whose closing balance is negative. Each qualifying daily record counts once.",
   "author_sql": "SELECT COUNT(*) AS account_day_count FROM account_daily WHERE currency = 'USD' AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' AND closing_minor < 0"
  },
  {
   "id": "P11",
   "author_order": 11,
   "kind": "analytical",
   "text": "How many CAD transactions occurred during July 2026, regardless of their status or whether they were posted? Return one column: transaction count.",
   "columns": [
    "transaction_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COUNT(*) FROM transactions WHERE currency='CAD' AND occurred_at>=TIMESTAMP '2026-07-01' AND occurred_at<TIMESTAMP '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     427
    ]
   ],
   "author_reading": "Count CAD transaction rows whose occurrence timestamp falls in July. Include every event kind and status.",
   "author_sql": "SELECT COUNT(*) AS transaction_count FROM transactions WHERE currency = 'CAD' AND occurred_at >= TIMESTAMP '2026-07-01 00:00:00' AND occurred_at < TIMESTAMP '2026-08-01 00:00:00'"
  },
  {
   "id": "P12",
   "author_order": 12,
   "kind": "analytical",
   "text": "How many USD transactions have a posting timestamp during July 2026, regardless of their recorded status or when they occurred? Return one column: transaction count.",
   "columns": [
    "transaction_count"
   ],
   "ordered": false,
   "gold_sql": "SELECT COUNT(*) FROM transactions WHERE currency='USD' AND posted_at>=TIMESTAMP '2026-07-01' AND posted_at<TIMESTAMP '2026-08-01'",
   "empty_key": false,
   "key_rows": [
    [
     168
    ]
   ],
   "author_reading": "Count USD transaction rows with a posting timestamp in July. The posting timestamp alone determines inclusion.",
   "author_sql": "SELECT COUNT(*) AS transaction_count FROM transactions WHERE currency = 'USD' AND posted_at >= TIMESTAMP '2026-07-01 00:00:00' AND posted_at < TIMESTAMP '2026-08-01 00:00:00'"
  },
  {
   "id": "P13",
   "author_order": 13,
   "kind": "analytical",
   "text": "For each day in July 2026 with a CAD card spending record, what were purchases, refunds and net spending? Return date as YYYY-MM-DD text, purchases in whole CAD cents, refunds in whole CAD cents and net spending in whole CAD cents, in that order. Sort by date ascending.",
   "columns": [
    "spend_date",
    "purchases_cents",
    "refunds_cents",
    "net_spend_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT STRFTIME(spend_date,'%Y-%m-%d'), SUM(purchased_minor), SUM(refunded_minor), SUM(net_spend_minor) FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY spend_date ORDER BY spend_date",
   "empty_key": false,
   "key_rows": [
    [
     "2026-07-01",
     66420,
     0,
     66420
    ],
    [
     "2026-07-02",
     48706,
     0,
     48706
    ],
    [
     "2026-07-03",
     29351,
     0,
     29351
    ],
    [
     "2026-07-04",
     43817,
     0,
     43817
    ],
    [
     "2026-07-05",
     27602,
     2345,
     25257
    ],
    [
     "2026-07-06",
     48767,
     1000,
     47767
    ],
    [
     "2026-07-07",
     54913,
     0,
     54913
    ],
    [
     "2026-07-08",
     37942,
     0,
     37942
    ],
    [
     "2026-07-09",
     56048,
     18301,
     37747
    ],
    [
     "2026-07-10",
     42269,
     0,
     42269
    ],
    [
     "2026-07-11",
     58518,
     0,
     58518
    ],
    [
     "2026-07-12",
     27667,
     0,
     27667
    ],
    [
     "2026-07-13",
     52979,
     0,
     52979
    ],
    [
     "2026-07-14",
     49789,
     0,
     49789
    ],
    [
     "2026-07-15",
     25343,
     0,
     25343
    ],
    [
     "2026-07-16",
     45872,
     16593,
     29279
    ],
    [
     "2026-07-17",
     41628,
     0,
     41628
    ],
    [
     "2026-07-18",
     46129,
     0,
     46129
    ],
    [
     "2026-07-19",
     34962,
     0,
     34962
    ],
    [
     "2026-07-20",
     43742,
     0,
     43742
    ],
    [
     "2026-07-21",
     40948,
     0,
     40948
    ],
    [
     "2026-07-22",
     45552,
     0,
     45552
    ],
    [
     "2026-07-23",
     40471,
     13645,
     26826
    ],
    [
     "2026-07-24",
     49751,
     0,
     49751
    ],
    [
     "2026-07-25",
     30147,
     0,
     30147
    ],
    [
     "2026-07-26",
     34592,
     0,
     34592
    ],
    [
     "2026-07-27",
     61314,
     0,
     61314
    ],
    [
     "2026-07-28",
     38322,
     0,
     38322
    ],
    [
     "2026-07-29",
     37432,
     0,
     37432
    ],
    [
     "2026-07-30",
     43591,
     12770,
     30821
    ],
    [
     "2026-07-31",
     38062,
     0,
     38062
    ]
   ],
   "author_reading": "Group July CAD daily card spending records by date and sum each recorded money measure. Include only dates represented in those records.",
   "author_sql": "SELECT strftime(spend_date, '%Y-%m-%d') AS spend_date, COALESCE(SUM(purchased_minor), 0) AS purchases_cents, COALESCE(SUM(refunded_minor), 0) AS refunds_cents, COALESCE(SUM(net_spend_minor), 0) AS net_spend_cents FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY spend_date ORDER BY spend_date ASC"
  },
  {
   "id": "P14",
   "author_order": 14,
   "kind": "analytical",
   "text": "For each currency represented in July 2026 daily account records, how much posted money came in and went out? Return currency, money in in whole cents of that currency and money out in whole cents of that currency, in that order. Sort by currency ascending.",
   "columns": [
    "currency",
    "money_in_cents",
    "money_out_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT currency, SUM(posted_credit_minor), SUM(posted_debit_minor) FROM account_daily WHERE balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' GROUP BY currency ORDER BY currency",
   "empty_key": false,
   "key_rows": [
    [
     "CAD",
     848160,
     1368640
    ],
    [
     "USD",
     362490,
     601192
    ]
   ],
   "author_reading": "Group July daily account records by currency. Use recorded posted credits for money in and recorded posted debits for money out.",
   "author_sql": "SELECT currency, COALESCE(SUM(posted_credit_minor), 0) AS money_in_cents, COALESCE(SUM(posted_debit_minor), 0) AS money_out_cents FROM account_daily WHERE balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' GROUP BY currency ORDER BY currency ASC"
  },
  {
   "id": "P15",
   "author_order": 15,
   "kind": "analytical",
   "text": "How did USD card spending break down by category reporting group during July 2026? Return reporting group, purchases in whole USD cents and refunds in whole USD cents, in that order. Include groups with matching spending records and sort by purchases descending, then reporting group ascending.",
   "columns": [
    "reporting_group",
    "purchases_cents",
    "refunds_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT c.reporting_group, SUM(s.purchased_minor) AS p, SUM(s.refunded_minor) FROM card_spend_daily s JOIN categories c ON c.category_id=s.category_id WHERE s.currency='USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY c.reporting_group ORDER BY p DESC, c.reporting_group",
   "empty_key": false,
   "key_rows": [
    [
     "Operations",
     339084,
     15039
    ],
    [
     "Technology",
     104648,
     3336
    ]
   ],
   "author_reading": "Join July USD daily card spending records to the category directory and aggregate by reporting group. Records without a matching category are excluded.",
   "author_sql": "SELECT c.reporting_group, COALESCE(SUM(s.purchased_minor), 0) AS purchases_cents, COALESCE(SUM(s.refunded_minor), 0) AS refunds_cents FROM card_spend_daily s JOIN categories c ON c.category_id = s.category_id WHERE s.currency = 'USD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY c.reporting_group ORDER BY purchases_cents DESC, c.reporting_group ASC NULLS LAST"
  },
  {
   "id": "P16",
   "author_order": 16,
   "kind": "analytical",
   "text": "Which five cards had the highest total CAD purchases during July 2026, among cards with at least one purchase? Return card identifier and purchases in whole CAD cents, in that order. Sort by purchases descending, breaking ties by card identifier ascending, and return at most five rows.",
   "columns": [
    "card_id",
    "purchases_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT card_id, SUM(purchased_minor) AS p FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id HAVING SUM(purchase_count)>0 ORDER BY p DESC, card_id LIMIT 5",
   "empty_key": false,
   "key_rows": [
    [
     "card_011_2",
     76927
    ],
    [
     "card_007_2",
     74310
    ],
    [
     "card_008_1",
     73794
    ],
    [
     "card_014_2",
     73237
    ],
    [
     "card_010_1",
     72616
    ]
   ],
   "author_reading": "Aggregate July CAD spending by card and retain cards whose total purchase count is positive. Rank by total recorded purchase amount with the card identifier deciding ties.",
   "author_sql": "SELECT card_id, COALESCE(SUM(purchased_minor), 0) AS purchases_cents FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id HAVING SUM(purchase_count) > 0 ORDER BY purchases_cents DESC, card_id ASC LIMIT 5"
  },
  {
   "id": "P17",
   "author_order": 17,
   "kind": "analytical",
   "text": "What was each USD account's closing balance on July 31, 2026, for accounts with a balance record that day? Return account identifier and closing balance in whole USD cents, in that order. Sort by closing balance ascending, then account identifier ascending.",
   "columns": [
    "account_id",
    "closing_balance_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT account_id, closing_minor FROM account_daily WHERE currency='USD' AND balance_date=DATE '2026-07-31' ORDER BY closing_minor, account_id",
   "empty_key": false,
   "key_rows": [
    [
     "acct_006",
     -9576
    ],
    [
     "acct_003",
     -8425
    ],
    [
     "acct_015",
     -3942
    ],
    [
     "acct_018",
     0
    ],
    [
     "acct_009",
     18566
    ],
    [
     "acct_012",
     23490
    ]
   ],
   "author_reading": "Read USD daily account balances dated July 31. Do not substitute another date for an account missing that day's balance.",
   "author_sql": "SELECT account_id, closing_minor AS closing_balance_cents FROM account_daily WHERE currency = 'USD' AND balance_date = DATE '2026-07-31' ORDER BY closing_balance_cents ASC, account_id ASC"
  },
  {
   "id": "P18",
   "author_order": 18,
   "kind": "analytical",
   "text": "What was net card spending for each organization and currency during July 2026? Return organization identifier, currency and net spending in whole cents of that currency, in that order. Include combinations with spending records linked to a listed account; sort by organization identifier ascending, then currency ascending.",
   "columns": [
    "organization_id",
    "currency",
    "net_spend_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT a.organization_id, s.currency, SUM(s.net_spend_minor) FROM card_spend_daily s JOIN accounts a ON a.account_id=s.account_id WHERE spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY a.organization_id, s.currency ORDER BY a.organization_id, s.currency",
   "empty_key": false,
   "key_rows": [
    [
     "org_01",
     "CAD",
     230735
    ],
    [
     "org_01",
     "USD",
     106600
    ],
    [
     "org_02",
     "CAD",
     203006
    ],
    [
     "org_02",
     "USD",
     127490
    ],
    [
     "org_03",
     "CAD",
     253869
    ],
    [
     "org_03",
     "USD",
     88313
    ],
    [
     "org_04",
     "CAD",
     250691
    ],
    [
     "org_04",
     "USD",
     103972
    ],
    [
     "org_05",
     "CAD",
     243071
    ],
    [
     "org_05",
     "USD",
     127957
    ],
    [
     "org_06",
     "CAD",
     96620
    ]
   ],
   "author_reading": "Link July daily card spending records to their accounts and aggregate by the account's organization and the spending currency. Keep currencies separate.",
   "author_sql": "SELECT a.organization_id, s.currency, COALESCE(SUM(s.net_spend_minor), 0) AS net_spend_cents FROM card_spend_daily s JOIN accounts a ON a.account_id = s.account_id WHERE s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY a.organization_id, s.currency ORDER BY a.organization_id ASC, s.currency ASC"
  },
  {
   "id": "P19",
   "author_order": 19,
   "kind": "analytical",
   "text": "Among accounts with at least one daily balance record during July 2026 in their listed currency, how many accounts are in each currently recorded account status and currency? Return account status, currency and account count, in that order. Sort by account status ascending, then currency ascending.",
   "columns": [
    "account_status",
    "currency",
    "account_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT a.account_status, a.currency, COUNT(*) FROM accounts a WHERE EXISTS (SELECT 1 FROM account_daily d WHERE d.account_id=a.account_id AND d.currency=a.currency AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01') GROUP BY a.account_status, a.currency ORDER BY a.account_status, a.currency",
   "empty_key": false,
   "key_rows": [
    [
     "closed",
     "USD",
     1
    ],
    [
     "open",
     "CAD",
     13
    ],
    [
     "open",
     "USD",
     5
    ]
   ],
   "author_reading": "Use the account directory's status as recorded, without treating it as historical status. Count each account once if it has a July balance record in its listed currency.",
   "author_sql": "SELECT a.account_status, a.currency, COUNT(DISTINCT a.account_id) AS account_count FROM accounts a WHERE EXISTS (SELECT 1 FROM account_daily d WHERE d.account_id = a.account_id AND d.currency = a.currency AND d.balance_date >= DATE '2026-07-01' AND d.balance_date < DATE '2026-08-01') GROUP BY a.account_status, a.currency ORDER BY a.account_status ASC NULLS LAST, a.currency ASC"
  },
  {
   "id": "P20",
   "author_order": 20,
   "kind": "analytical",
   "text": "How many CAD card purchases were recorded in each Monday-starting week touched by July 2026? Count only July dates and include only weeks with CAD spending records. Return the week's Monday as YYYY-MM-DD text and purchase count, in that order. Sort by the week's Monday ascending.",
   "columns": [
    "week_start",
    "purchase_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT STRFTIME(CAST(DATE_TRUNC('week', spend_date) AS DATE),'%Y-%m-%d') AS w, SUM(purchase_count) FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY w ORDER BY w",
   "empty_key": false,
   "key_rows": [
    [
     "2026-06-29",
     50
    ],
    [
     "2026-07-06",
     61
    ],
    [
     "2026-07-13",
     66
    ],
    [
     "2026-07-20",
     63
    ],
    [
     "2026-07-27",
     46
    ]
   ],
   "author_reading": "Restrict daily spending records to July CAD activity before grouping into Monday-starting weeks. The first and last weeks contain only their July dates.",
   "author_sql": "SELECT strftime(CAST(date_trunc('week', spend_date) AS DATE), '%Y-%m-%d') AS week_start, COALESCE(SUM(purchase_count), 0) AS purchase_count FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY date_trunc('week', spend_date) ORDER BY week_start ASC"
  },
  {
   "id": "P21",
   "author_order": 21,
   "kind": "analytical",
   "text": "For transactions that occurred during July 2026, how many were recorded under each event type, status and currency combination? Return event type, status, currency and transaction count, in that order. Include all statuses and sort by event type, then status, then currency, all ascending with missing labels last.",
   "columns": [
    "event_type",
    "status",
    "currency",
    "transaction_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT event_kind, status, currency, COUNT(*) FROM transactions WHERE occurred_at>=TIMESTAMP '2026-07-01' AND occurred_at<TIMESTAMP '2026-08-01' GROUP BY event_kind, status, currency ORDER BY event_kind NULLS LAST, status NULLS LAST, currency NULLS LAST",
   "empty_key": false,
   "key_rows": [
    [
     "deposit",
     "posted",
     "CAD",
     24
    ],
    [
     "deposit",
     "posted",
     "USD",
     10
    ],
    [
     "purchase",
     "pending",
     "CAD",
     31
    ],
    [
     "purchase",
     "pending",
     "USD",
     14
    ],
    [
     "purchase",
     "posted",
     "CAD",
     286
    ],
    [
     "purchase",
     "posted",
     "USD",
     129
    ],
    [
     "purchase",
     "void",
     "CAD",
     25
    ],
    [
     "purchase",
     "void",
     "USD",
     12
    ],
    [
     "refund",
     "posted",
     "CAD",
     39
    ],
    [
     "refund",
     "posted",
     "USD",
     16
    ],
    [
     "transfer",
     "pending",
     "CAD",
     2
    ],
    [
     "transfer",
     "posted",
     "CAD",
     8
    ],
    [
     "transfer",
     "posted",
     "USD",
     8
    ],
    [
     "transfer",
     "void",
     "USD",
     2
    ],
    [
     "withdrawal",
     "posted",
     "CAD",
     12
    ],
    [
     "withdrawal",
     "posted",
     "USD",
     5
    ]
   ],
   "author_reading": "Group all transactions with a July occurrence timestamp by their recorded event kind, status and currency. No meanings or allowed values are assumed for these labels.",
   "author_sql": "SELECT event_kind AS event_type, status, currency, COUNT(*) AS transaction_count FROM transactions WHERE occurred_at >= TIMESTAMP '2026-07-01 00:00:00' AND occurred_at < TIMESTAMP '2026-08-01 00:00:00' GROUP BY event_kind, status, currency ORDER BY event_type ASC NULLS LAST, status ASC NULLS LAST, currency ASC NULLS LAST"
  },
  {
   "id": "P22",
   "author_order": 22,
   "kind": "analytical",
   "text": "For transactions with posting timestamps during July 2026, what were the count and total recorded amount under each direction label and currency? Include all statuses. Return direction label, currency, transaction count and recorded amount in whole cents of that currency, in that order. Sum amounts as stored without changing their signs; sort by direction label ascending, then currency ascending, with missing labels last.",
   "columns": [
    "direction",
    "currency",
    "transaction_count",
    "recorded_amount_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT direction, currency, COUNT(*), SUM(amount_minor) FROM transactions WHERE posted_at>=TIMESTAMP '2026-07-01' AND posted_at<TIMESTAMP '2026-08-01' GROUP BY direction, currency ORDER BY direction NULLS LAST, currency NULLS LAST",
   "empty_key": false,
   "key_rows": [
    [
     "credit",
     "CAD",
     67,
     848160
    ],
    [
     "credit",
     "USD",
     30,
     362490
    ],
    [
     "debit",
     "CAD",
     302,
     1368640
    ],
    [
     "debit",
     "USD",
     138,
     601192
    ]
   ],
   "author_reading": "Group transactions by recorded direction and currency using July posting timestamps. Add the stored amounts directly rather than inferring a sign from the direction label.",
   "author_sql": "SELECT direction, currency, COUNT(*) AS transaction_count, COALESCE(SUM(amount_minor), 0) AS recorded_amount_cents FROM transactions WHERE posted_at >= TIMESTAMP '2026-07-01 00:00:00' AND posted_at < TIMESTAMP '2026-08-01 00:00:00' GROUP BY direction, currency ORDER BY direction ASC NULLS LAST, currency ASC NULLS LAST"
  },
  {
   "id": "P23",
   "author_order": 23,
   "kind": "analytical",
   "text": "Which ten categories had the most CAD card refunds during July 2026, among categories with at least one refund and a matching category listing? Return category identifier, category name, refund count and refunds in whole CAD cents, in that order. Sort by refund count descending, breaking ties by category identifier ascending, and return at most ten rows.",
   "columns": [
    "category_id",
    "category_name",
    "refund_count",
    "refunds_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT c.category_id, c.category_name, SUM(s.refund_count) AS n, SUM(s.refunded_minor) FROM card_spend_daily s JOIN categories c ON c.category_id=s.category_id WHERE s.currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY c.category_id, c.category_name HAVING SUM(s.refund_count)>0 ORDER BY n DESC, c.category_id LIMIT 10",
   "empty_key": false,
   "key_rows": [
    [
     "cat_software",
     "Software",
     8,
     13759
    ],
    [
     "cat_supplies",
     "Supplies",
     8,
     13874
    ],
    [
     "cat_meals",
     "Meals",
     7,
     15360
    ],
    [
     "cat_travel",
     "Travel",
     7,
     9628
    ]
   ],
   "author_reading": "Aggregate July CAD card records by their matched category. Rank categories by total refund count, using the category identifier to resolve ties.",
   "author_sql": "SELECT c.category_id, c.category_name, COALESCE(SUM(s.refund_count), 0) AS refund_count, COALESCE(SUM(s.refunded_minor), 0) AS refunds_cents FROM card_spend_daily s JOIN categories c ON c.category_id = s.category_id WHERE s.currency = 'CAD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY c.category_id, c.category_name HAVING SUM(s.refund_count) > 0 ORDER BY refund_count DESC, c.category_id ASC LIMIT 10"
  },
  {
   "id": "P24",
   "author_order": 24,
   "kind": "analytical",
   "text": "For each organization with CAD accounts having both a July 1, 2026 opening balance record and a July 31, 2026 closing balance record, how much did those accounts' combined balance change over July? Include only accounts with both records. Return organization identifier, included account count and balance change in whole CAD cents, calculated as closing minus opening, in that order. Sort by balance change descending, then organization identifier ascending.",
   "columns": [
    "organization_id",
    "account_count",
    "balance_change_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT a.organization_id, COUNT(*), SUM(e.closing_minor - o.opening_minor) AS chg FROM accounts a JOIN account_daily o ON o.account_id=a.account_id AND o.balance_date=DATE '2026-07-01' JOIN account_daily e ON e.account_id=a.account_id AND e.balance_date=DATE '2026-07-31' WHERE a.currency='CAD' GROUP BY a.organization_id ORDER BY chg DESC, a.organization_id",
   "empty_key": false,
   "key_rows": [
    [
     "org_06",
     2,
     32842
    ],
    [
     "org_07",
     1,
     0
    ],
    [
     "org_02",
     2,
     -78680
    ],
    [
     "org_01",
     2,
     -107693
    ],
    [
     "org_05",
     2,
     -114893
    ],
    [
     "org_04",
     2,
     -123797
    ],
    [
     "org_03",
     2,
     -128259
    ]
   ],
   "author_reading": "Match each listed CAD account's July 1 opening balance to its July 31 closing balance, then sum account-level changes by organization. An account must have both snapshots to contribute.",
   "author_sql": "SELECT a.organization_id, COUNT(*) AS account_count, SUM(e.closing_minor - b.opening_minor) AS balance_change_cents FROM accounts a JOIN account_daily b ON b.account_id = a.account_id AND b.currency = 'CAD' AND b.balance_date = DATE '2026-07-01' JOIN account_daily e ON e.account_id = a.account_id AND e.currency = 'CAD' AND e.balance_date = DATE '2026-07-31' WHERE a.currency = 'CAD' GROUP BY a.organization_id ORDER BY balance_change_cents DESC, a.organization_id ASC"
  },
  {
   "id": "P25",
   "author_order": 25,
   "kind": "analytical",
   "text": "For each card with at least one USD purchase during July 2026, how many distinct nonmissing category identifiers had purchases? Return card identifier and category count, in that order. Sort by category count descending, then card identifier ascending.",
   "columns": [
    "card_id",
    "category_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT card_id, COUNT(DISTINCT category_id) AS n FROM card_spend_daily WHERE currency='USD' AND purchase_count>0 AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id ORDER BY n DESC, card_id",
   "empty_key": false,
   "key_rows": [
    [
     "card_003_1",
     4
    ],
    [
     "card_003_2",
     4
    ],
    [
     "card_006_1",
     4
    ],
    [
     "card_006_2",
     4
    ],
    [
     "card_009_1",
     4
    ],
    [
     "card_009_2",
     4
    ],
    [
     "card_012_1",
     4
    ],
    [
     "card_012_2",
     4
    ],
    [
     "card_015_1",
     4
    ],
    [
     "card_015_2",
     4
    ]
   ],
   "author_reading": "Use July USD daily spending rows with positive purchase counts. Count distinct nonnull category identifiers per card, including cards whose resulting category count is zero.",
   "author_sql": "SELECT card_id, COUNT(DISTINCT category_id) AS category_count FROM card_spend_daily WHERE currency = 'USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' AND purchase_count > 0 GROUP BY card_id ORDER BY category_count DESC, card_id ASC"
  },
  {
   "id": "P26",
   "author_order": 26,
   "kind": "analytical",
   "text": "For each CAD account that ended at least one recorded day below zero during July 2026, on how many distinct dates did it do so? Return account identifier and negative-balance day count, in that order. Sort by day count descending, then account identifier ascending.",
   "columns": [
    "account_id",
    "negative_day_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT account_id, COUNT(DISTINCT balance_date) AS n FROM account_daily WHERE currency='CAD' AND closing_minor<0 AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' GROUP BY account_id ORDER BY n DESC, account_id",
   "empty_key": false,
   "key_rows": [
    [
     "acct_001",
     11
    ],
    [
     "acct_008",
     6
    ],
    [
     "acct_010",
     5
    ],
    [
     "acct_013",
     4
    ],
    [
     "acct_007",
     3
    ],
    [
     "acct_011",
     3
    ],
    [
     "acct_004",
     2
    ]
   ],
   "author_reading": "Restrict July CAD daily balances to negative closing balances and count distinct dates per account. Exclude accounts with no negative closing date.",
   "author_sql": "SELECT account_id, COUNT(DISTINCT balance_date) AS negative_day_count FROM account_daily WHERE currency = 'CAD' AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' AND closing_minor < 0 GROUP BY account_id ORDER BY negative_day_count DESC, account_id ASC"
  },
  {
   "id": "P27",
   "author_order": 27,
   "kind": "analytical",
   "text": "For each date and currency in July 2026 with at least one daily account balance that fails opening balance plus posted money in minus posted money out equals closing balance, how many account records fail? Consider only records with all four amounts present. Return date as YYYY-MM-DD text, currency and failing record count, in that order. Sort by date ascending, then currency ascending.",
   "columns": [
    "balance_date",
    "currency",
    "failing_record_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT STRFTIME(balance_date,'%Y-%m-%d'), currency, COUNT(*) FROM account_daily WHERE balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' AND opening_minor IS NOT NULL AND posted_credit_minor IS NOT NULL AND posted_debit_minor IS NOT NULL AND closing_minor IS NOT NULL AND opening_minor+posted_credit_minor-posted_debit_minor<>closing_minor GROUP BY balance_date, currency ORDER BY balance_date, currency",
   "empty_key": true,
   "key_rows": [],
   "author_reading": "Compare the four recorded daily balance amounts directly, using posted credits as money in and posted debits as money out. Count only complete records where the equality fails.",
   "author_sql": "SELECT strftime(balance_date, '%Y-%m-%d') AS balance_date, currency, COUNT(*) AS failing_record_count FROM account_daily WHERE balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01' AND opening_minor IS NOT NULL AND posted_credit_minor IS NOT NULL AND posted_debit_minor IS NOT NULL AND closing_minor IS NOT NULL AND opening_minor + posted_credit_minor - posted_debit_minor <> closing_minor GROUP BY balance_date, currency ORDER BY balance_date ASC, currency ASC"
  },
  {
   "id": "P28",
   "author_order": 28,
   "kind": "analytical",
   "text": "How many card purchases and refunds were recorded during July 2026 under each currently recorded card status and currency? Include status and currency combinations with spending records linked to a listed card. Return card status, currency, purchase count and refund count, in that order. Sort by card status ascending, then currency ascending, with missing statuses last.",
   "columns": [
    "card_status",
    "currency",
    "purchase_count",
    "refund_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT c.card_status, s.currency, SUM(s.purchase_count), SUM(s.refund_count) FROM card_spend_daily s JOIN cards c ON c.card_id=s.card_id WHERE spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY c.card_status, s.currency ORDER BY c.card_status NULLS LAST, s.currency",
   "empty_key": false,
   "key_rows": [
    [
     "active",
     "CAD",
     260,
     36
    ],
    [
     "active",
     "USD",
     62,
     9
    ],
    [
     "cancelled",
     "CAD",
     26,
     3
    ],
    [
     "cancelled",
     "USD",
     13,
     1
    ],
    [
     "frozen",
     "USD",
     54,
     6
    ]
   ],
   "author_reading": "Join July daily card spending to the card directory and aggregate its purchase and refund counts. The card status is the directory's recorded status, not an inferred historical status.",
   "author_sql": "SELECT c.card_status, s.currency, COALESCE(SUM(s.purchase_count), 0) AS purchase_count, COALESCE(SUM(s.refund_count), 0) AS refund_count FROM card_spend_daily s JOIN cards c ON c.card_id = s.card_id WHERE s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY c.card_status, s.currency ORDER BY c.card_status ASC NULLS LAST, s.currency ASC"
  },
  {
   "id": "P29",
   "author_order": 29,
   "kind": "analytical",
   "text": "Which listed CAD accounts have at least one daily balance record during July 2026 but no recorded CAD card purchases during that month? Treat a positive purchase count on any daily card record as evidence of purchasing. Return organization identifier and account identifier, in that order. Sort by organization identifier ascending, then account identifier ascending.",
   "columns": [
    "organization_id",
    "account_id"
   ],
   "ordered": true,
   "gold_sql": "SELECT a.organization_id, a.account_id FROM accounts a WHERE a.currency='CAD' AND EXISTS (SELECT 1 FROM account_daily d WHERE d.account_id=a.account_id AND balance_date >= DATE '2026-07-01' AND balance_date < DATE '2026-08-01') AND NOT EXISTS (SELECT 1 FROM card_spend_daily s WHERE s.account_id=a.account_id AND s.currency='CAD' AND s.purchase_count>0 AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01') ORDER BY a.organization_id, a.account_id",
   "empty_key": false,
   "key_rows": [
    [
     "org_06",
     "acct_017"
    ],
    [
     "org_07",
     "acct_019"
    ]
   ],
   "author_reading": "Start with listed CAD accounts observed in July's CAD daily balances. Retain accounts with no July CAD daily spending row containing a positive purchase count.",
   "author_sql": "SELECT a.organization_id, a.account_id FROM accounts a WHERE a.currency = 'CAD' AND EXISTS (SELECT 1 FROM account_daily d WHERE d.account_id = a.account_id AND d.currency = 'CAD' AND d.balance_date >= DATE '2026-07-01' AND d.balance_date < DATE '2026-08-01') AND NOT EXISTS (SELECT 1 FROM card_spend_daily s WHERE s.account_id = a.account_id AND s.currency = 'CAD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' AND s.purchase_count > 0) ORDER BY a.organization_id ASC, a.account_id ASC"
  },
  {
   "id": "P30",
   "author_order": 30,
   "kind": "analytical",
   "text": "Which listed cards on USD accounts had neither a recorded USD purchase nor a recorded USD refund during July 2026? Use positive purchase or refund counts to identify activity, and include cards with no daily spending records. Return account identifier, card identifier and currently recorded card status, in that order. Sort by account identifier ascending, then card identifier ascending.",
   "columns": [
    "account_id",
    "card_id",
    "card_status"
   ],
   "ordered": true,
   "gold_sql": "SELECT c.account_id, c.card_id, c.card_status FROM cards c JOIN accounts a ON a.account_id=c.account_id WHERE a.currency='USD' AND NOT EXISTS (SELECT 1 FROM card_spend_daily s WHERE s.card_id=c.card_id AND s.currency='USD' AND (s.purchase_count>0 OR s.refund_count>0) AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01') ORDER BY c.account_id, c.card_id",
   "empty_key": true,
   "key_rows": [],
   "author_reading": "Use the listed card inventory and each card's listed USD account, without inferring when the card existed. Exclude cards with any July USD daily record showing a positive purchase or refund count.",
   "author_sql": "SELECT c.account_id, c.card_id, c.card_status FROM cards c JOIN accounts a ON a.account_id = c.account_id WHERE a.currency = 'USD' AND NOT EXISTS (SELECT 1 FROM card_spend_daily s WHERE s.card_id = c.card_id AND s.currency = 'USD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' AND (s.purchase_count > 0 OR s.refund_count > 0)) ORDER BY c.account_id ASC, c.card_id ASC"
  },
  {
   "id": "P31",
   "author_order": 31,
   "kind": "analytical",
   "text": "Which organizations recorded at least one card purchase in each of CAD and USD during July 2026? Return organization identifier, CAD purchases in whole CAD cents and USD purchases in whole USD cents, in that order. Include all recorded purchase amounts in each currency's total and sort by organization identifier ascending.",
   "columns": [
    "organization_id",
    "cad_purchases_cents",
    "usd_purchases_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT a.organization_id, SUM(CASE WHEN s.currency='CAD' THEN s.purchased_minor ELSE 0 END), SUM(CASE WHEN s.currency='USD' THEN s.purchased_minor ELSE 0 END) FROM card_spend_daily s JOIN accounts a ON a.account_id=s.account_id WHERE spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY a.organization_id HAVING SUM(CASE WHEN s.currency='CAD' THEN s.purchase_count ELSE 0 END)>0 AND SUM(CASE WHEN s.currency='USD' THEN s.purchase_count ELSE 0 END)>0 ORDER BY a.organization_id",
   "empty_key": false,
   "key_rows": [
    [
     "org_01",
     245277,
     113105
    ],
    [
     "org_02",
     211447,
     133490
    ],
    [
     "org_03",
     269590,
     94275
    ],
    [
     "org_04",
     259427,
     108178
    ],
    [
     "org_05",
     256624,
     131511
    ]
   ],
   "author_reading": "Link July card spending to listed accounts, then require a positive purchase count in at least one daily record for each currency. Sum purchase amounts separately for CAD and USD.",
   "author_sql": "SELECT a.organization_id, COALESCE(SUM(CASE WHEN s.currency = 'CAD' THEN s.purchased_minor ELSE 0 END), 0) AS cad_purchases_cents, COALESCE(SUM(CASE WHEN s.currency = 'USD' THEN s.purchased_minor ELSE 0 END), 0) AS usd_purchases_cents FROM card_spend_daily s JOIN accounts a ON a.account_id = s.account_id WHERE s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' AND s.currency IN ('CAD', 'USD') GROUP BY a.organization_id HAVING COUNT(CASE WHEN s.currency = 'CAD' AND s.purchase_count > 0 THEN 1 END) > 0 AND COUNT(CASE WHEN s.currency = 'USD' AND s.purchase_count > 0 THEN 1 END) > 0 ORDER BY a.organization_id ASC"
  },
  {
   "id": "P32",
   "author_order": 32,
   "kind": "analytical",
   "text": "Which accounts had CAD transactions that occurred during July 2026 but received posting timestamps from August 1 through August 3, 2026? Include every event type and status. Return account identifier, transaction count and total recorded amount in whole CAD cents, summing amounts as stored, in that order. Sort by transaction count descending, then account identifier ascending.",
   "columns": [
    "account_id",
    "transaction_count",
    "recorded_amount_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT account_id, COUNT(*) AS n, SUM(amount_minor) FROM transactions WHERE currency='CAD' AND occurred_at>=TIMESTAMP '2026-07-01' AND occurred_at<TIMESTAMP '2026-08-01' AND posted_at>=TIMESTAMP '2026-08-01' AND posted_at<TIMESTAMP '2026-08-04' GROUP BY account_id ORDER BY n DESC, account_id",
   "empty_key": false,
   "key_rows": [
    [
     "acct_001",
     1,
     4567
    ]
   ],
   "author_reading": "Select CAD transactions with July occurrence timestamps and posting timestamps in the first three days of August. Group by account and sum stored amounts without inferring signs or filtering statuses.",
   "author_sql": "SELECT account_id, COUNT(*) AS transaction_count, COALESCE(SUM(amount_minor), 0) AS recorded_amount_cents FROM transactions WHERE currency = 'CAD' AND occurred_at >= TIMESTAMP '2026-07-01 00:00:00' AND occurred_at < TIMESTAMP '2026-08-01 00:00:00' AND posted_at >= TIMESTAMP '2026-08-01 00:00:00' AND posted_at < TIMESTAMP '2026-08-04 00:00:00' GROUP BY account_id ORDER BY transaction_count DESC, account_id ASC"
  },
  {
   "id": "P33",
   "author_order": 33,
   "kind": "analytical",
   "text": "Which cards recorded CAD purchases during July 2026 but no CAD refunds during that month? A positive daily purchase or refund count establishes the corresponding activity. Return card identifier, purchase count and purchases in whole CAD cents, in that order. Sort by purchases descending, then card identifier ascending.",
   "columns": [
    "card_id",
    "purchase_count",
    "purchases_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT card_id, SUM(purchase_count), SUM(purchased_minor) AS p FROM card_spend_daily WHERE currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id HAVING SUM(purchase_count)>0 AND SUM(refund_count)=0 ORDER BY p DESC, card_id",
   "empty_key": true,
   "key_rows": [],
   "author_reading": "Aggregate July CAD daily card records, requiring at least one positive purchase count and no positive refund count. Return the full monthly purchase totals for qualifying cards.",
   "author_sql": "SELECT card_id, COALESCE(SUM(purchase_count), 0) AS purchase_count, COALESCE(SUM(purchased_minor), 0) AS purchases_cents FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id HAVING COUNT(CASE WHEN purchase_count > 0 THEN 1 END) > 0 AND COUNT(CASE WHEN refund_count > 0 THEN 1 END) = 0 ORDER BY purchases_cents DESC, card_id ASC"
  },
  {
   "id": "P34",
   "author_order": 34,
   "kind": "analytical",
   "text": "Which listed categories had more USD card refunds than purchases by amount during July 2026? Return category identifier, category name, purchases in whole USD cents, refunds in whole USD cents and excess refunds in whole USD cents, calculated as refunds minus purchases, in that order. Sort by excess refunds descending, then category identifier ascending.",
   "columns": [
    "category_id",
    "category_name",
    "purchases_cents",
    "refunds_cents",
    "excess_refunds_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT c.category_id, c.category_name, SUM(s.purchased_minor), SUM(s.refunded_minor), SUM(s.refunded_minor)-SUM(s.purchased_minor) AS x FROM card_spend_daily s JOIN categories c ON c.category_id=s.category_id WHERE s.currency='USD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY c.category_id, c.category_name HAVING SUM(s.refunded_minor)>SUM(s.purchased_minor) ORDER BY x DESC, c.category_id",
   "empty_key": true,
   "key_rows": [],
   "author_reading": "Aggregate July USD daily spending records by matched category and compare monthly refund and purchase totals. Refunds need not relate to purchases made in July.",
   "author_sql": "SELECT c.category_id, c.category_name, COALESCE(SUM(s.purchased_minor), 0) AS purchases_cents, COALESCE(SUM(s.refunded_minor), 0) AS refunds_cents, COALESCE(SUM(s.refunded_minor), 0) - COALESCE(SUM(s.purchased_minor), 0) AS excess_refunds_cents FROM card_spend_daily s JOIN categories c ON c.category_id = s.category_id WHERE s.currency = 'USD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY c.category_id, c.category_name HAVING COALESCE(SUM(s.refunded_minor), 0) > COALESCE(SUM(s.purchased_minor), 0) ORDER BY excess_refunds_cents DESC, c.category_id ASC"
  },
  {
   "id": "P35",
   "author_order": 35,
   "kind": "analytical",
   "text": "Which organizations had more than 100000 whole CAD cents of net card spending during July 2026, spread across at least two accounts that each recorded a CAD purchase? Return organization identifier, number of accounts with purchases and net spending in whole CAD cents, in that order. Include all July CAD net spending linked to the organization; sort by net spending descending, then organization identifier ascending.",
   "columns": [
    "organization_id",
    "purchasing_account_count",
    "net_spend_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT a.organization_id, COUNT(DISTINCT CASE WHEN s.purchase_count>0 THEN s.account_id END), SUM(s.net_spend_minor) AS n FROM card_spend_daily s JOIN accounts a ON a.account_id=s.account_id WHERE s.currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY a.organization_id HAVING SUM(s.net_spend_minor)>100000 AND COUNT(DISTINCT CASE WHEN s.purchase_count>0 THEN s.account_id END)>=2 ORDER BY n DESC, a.organization_id",
   "empty_key": false,
   "key_rows": [
    [
     "org_03",
     2,
     253869
    ],
    [
     "org_04",
     2,
     250691
    ],
    [
     "org_05",
     2,
     243071
    ],
    [
     "org_01",
     2,
     230735
    ],
    [
     "org_02",
     2,
     203006
    ]
   ],
   "author_reading": "Aggregate all July CAD daily card records linked to listed accounts by organization. Require net spending above the stated threshold and at least two distinct accounts with a positive purchase count.",
   "author_sql": "SELECT a.organization_id, COUNT(DISTINCT CASE WHEN s.purchase_count > 0 THEN s.account_id END) AS purchasing_account_count, COALESCE(SUM(s.net_spend_minor), 0) AS net_spend_cents FROM card_spend_daily s JOIN accounts a ON a.account_id = s.account_id WHERE s.currency = 'CAD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY a.organization_id HAVING COALESCE(SUM(s.net_spend_minor), 0) > 100000 AND COUNT(DISTINCT CASE WHEN s.purchase_count > 0 THEN s.account_id END) >= 2 ORDER BY net_spend_cents DESC, a.organization_id ASC"
  },
  {
   "id": "P36",
   "author_order": 36,
   "kind": "analytical",
   "text": "Which USD accounts ended both July 1 and July 31, 2026 below zero? Return account identifier, July 1 closing balance in whole USD cents and July 31 closing balance in whole USD cents, in that order. Sort by July 31 closing balance ascending, then account identifier ascending.",
   "columns": [
    "account_id",
    "july_1_closing_cents",
    "july_31_closing_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT o.account_id, o.closing_minor, e.closing_minor FROM account_daily o JOIN account_daily e ON e.account_id=o.account_id WHERE o.currency='USD' AND o.balance_date=DATE '2026-07-01' AND e.balance_date=DATE '2026-07-31' AND o.closing_minor<0 AND e.closing_minor<0 ORDER BY e.closing_minor, o.account_id",
   "empty_key": true,
   "key_rows": [],
   "author_reading": "Match USD daily balance records for the same account on the two specified dates. Require both closing balances to be negative; intermediate dates do not affect eligibility.",
   "author_sql": "SELECT b.account_id, b.closing_minor AS july_1_closing_cents, e.closing_minor AS july_31_closing_cents FROM account_daily b JOIN account_daily e ON e.account_id = b.account_id AND e.currency = b.currency AND e.balance_date = DATE '2026-07-31' WHERE b.currency = 'USD' AND b.balance_date = DATE '2026-07-01' AND b.closing_minor < 0 AND e.closing_minor < 0 ORDER BY july_31_closing_cents ASC, b.account_id ASC"
  },
  {
   "id": "P37",
   "author_order": 37,
   "kind": "analytical",
   "text": "Which cards recorded at least one CAD purchase on every calendar day of July 2026? Return card identifier and total July purchase count, in that order. Sort by purchase count descending, then card identifier ascending.",
   "columns": [
    "card_id",
    "purchase_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT card_id, SUM(purchase_count) AS n FROM card_spend_daily WHERE currency='CAD' AND purchase_count>0 AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id HAVING COUNT(DISTINCT spend_date)=31 ORDER BY n DESC, card_id",
   "empty_key": true,
   "key_rows": [],
   "author_reading": "Group July CAD daily card records by card and require positive purchase counts on all 31 distinct July dates. Sum all recorded July purchase counts for each qualifying card.",
   "author_sql": "SELECT card_id, COALESCE(SUM(purchase_count), 0) AS purchase_count FROM card_spend_daily WHERE currency = 'CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY card_id HAVING COUNT(DISTINCT CASE WHEN purchase_count > 0 THEN spend_date END) = 31 ORDER BY purchase_count DESC, card_id ASC"
  },
  {
   "id": "P38",
   "author_order": 40,
   "kind": "analytical",
   "text": "For each organization with a card that recorded a CAD purchase during July 2026, which single card had the highest total CAD purchases? Return organization identifier, winning card identifier and that card's purchases in whole CAD cents, in that order. Consider cards linked through their listed accounts, break equal purchase totals by the smallest card identifier, and sort the result by organization identifier ascending.",
   "columns": [
    "organization_id",
    "card_id",
    "purchases_cents"
   ],
   "ordered": true,
   "gold_sql": "WITH t AS (SELECT a.organization_id AS org, s.card_id AS card, SUM(s.purchased_minor) AS p FROM card_spend_daily s JOIN cards k ON k.card_id=s.card_id JOIN accounts a ON a.account_id=k.account_id WHERE s.currency='CAD' AND spend_date >= DATE '2026-07-01' AND spend_date < DATE '2026-08-01' GROUP BY a.organization_id, s.card_id HAVING SUM(s.purchase_count)>0), m AS (SELECT org, MAX(p) AS mp FROM t GROUP BY org) SELECT t.org, MIN(t.card), m.mp FROM t JOIN m ON m.org=t.org AND m.mp=t.p GROUP BY t.org, m.mp ORDER BY t.org",
   "empty_key": false,
   "key_rows": [
    [
     "org_01",
     "card_001_1",
     71928
    ],
    [
     "org_02",
     "card_004_2",
     57831
    ],
    [
     "org_03",
     "card_007_2",
     74310
    ],
    [
     "org_04",
     "card_011_2",
     76927
    ],
    [
     "org_05",
     "card_014_2",
     73237
    ],
    [
     "org_06",
     "card_016_2",
     60890
    ]
   ],
   "author_reading": "Use the card directory to assign cards to listed accounts and organizations, then total July CAD purchases for cards with a positive purchase count. Keep the highest total per organization, resolving ties by ascending card identifier.",
   "author_sql": "WITH totals AS (SELECT a.organization_id, c.card_id, COALESCE(SUM(s.purchased_minor), 0) AS purchases_cents FROM cards c JOIN accounts a ON a.account_id = c.account_id JOIN card_spend_daily s ON s.card_id = c.card_id AND s.account_id = c.account_id WHERE s.currency = 'CAD' AND s.spend_date >= DATE '2026-07-01' AND s.spend_date < DATE '2026-08-01' GROUP BY a.organization_id, c.card_id HAVING COUNT(CASE WHEN s.purchase_count > 0 THEN 1 END) > 0) SELECT t.organization_id, t.card_id, t.purchases_cents FROM totals t WHERE NOT EXISTS (SELECT 1 FROM totals x WHERE x.organization_id IS NOT DISTINCT FROM t.organization_id AND (x.purchases_cents > t.purchases_cents OR (x.purchases_cents = t.purchases_cents AND x.card_id < t.card_id))) ORDER BY t.organization_id ASC"
  },
  {
   "id": "P39",
   "author_order": 41,
   "kind": "analytical",
   "text": "Which transfer groups contain CAD transactions with July 2026 posting timestamps on at least two distinct accounts? Include all event types and statuses, and count only the July-posted CAD transactions within each group. Return transfer group identifier, distinct account count and transaction count, in that order. Exclude missing transfer group identifiers; sort by account count descending, then transfer group identifier ascending.",
   "columns": [
    "transfer_group_id",
    "account_count",
    "transaction_count"
   ],
   "ordered": true,
   "gold_sql": "SELECT transfer_group_id, COUNT(DISTINCT account_id) AS a, COUNT(*) AS n FROM transactions WHERE currency='CAD' AND transfer_group_id IS NOT NULL AND posted_at>=TIMESTAMP '2026-07-01' AND posted_at<TIMESTAMP '2026-08-01' GROUP BY transfer_group_id HAVING COUNT(DISTINCT account_id)>=2 ORDER BY a DESC, n DESC, transfer_group_id",
   "empty_key": false,
   "key_rows": [
    [
     "transfer_23_0",
     2,
     2
    ],
    [
     "transfer_23_1",
     2,
     2
    ],
    [
     "transfer_9_0",
     2,
     2
    ],
    [
     "transfer_9_1",
     2,
     2
    ]
   ],
   "author_reading": "Group CAD transactions with July posting timestamps by their nonnull transfer group identifier. Require at least two distinct account identifiers within that filtered set, without assuming that a transfer group has exactly two entries.",
   "author_sql": "SELECT transfer_group_id, COUNT(DISTINCT account_id) AS account_count, COUNT(*) AS transaction_count FROM transactions WHERE currency = 'CAD' AND posted_at >= TIMESTAMP '2026-07-01 00:00:00' AND posted_at < TIMESTAMP '2026-08-01 00:00:00' AND transfer_group_id IS NOT NULL GROUP BY transfer_group_id HAVING COUNT(DISTINCT account_id) >= 2 ORDER BY account_count DESC, transfer_group_id ASC"
  },
  {
   "id": "P40",
   "author_order": 44,
   "kind": "analytical",
   "text": "What were the ten largest declines in USD account closing balances from one calendar day to the next, with both dates within July 2026? Include only accounts with records on both consecutive dates and only positive declines. Return account identifier, later date as YYYY-MM-DD text, previous day's closing balance in whole USD cents, later day's closing balance in whole USD cents and decline in whole USD cents, calculated as previous minus later, in that order. Sort by decline descending, then account identifier ascending, then later date ascending, and return at most ten rows.",
   "columns": [
    "account_id",
    "later_date",
    "previous_closing_cents",
    "later_closing_cents",
    "decline_cents"
   ],
   "ordered": true,
   "gold_sql": "SELECT e.account_id, STRFTIME(e.balance_date,'%Y-%m-%d') AS dt, o.closing_minor, e.closing_minor, o.closing_minor-e.closing_minor AS x FROM account_daily o JOIN account_daily e ON e.account_id=o.account_id AND e.balance_date=o.balance_date+1 WHERE o.currency='USD' AND o.balance_date>=DATE '2026-07-01' AND e.balance_date<DATE '2026-08-01' AND o.closing_minor>e.closing_minor ORDER BY x DESC, e.account_id, dt LIMIT 10",
   "empty_key": false,
   "key_rows": [
    [
     "acct_009",
     "2026-07-23",
     49778,
     38824,
     10954
    ],
    [
     "acct_006",
     "2026-07-08",
     48841,
     38965,
     9876
    ],
    [
     "acct_009",
     "2026-07-06",
     65702,
     55841,
     9861
    ],
    [
     "acct_006",
     "2026-07-10",
     35674,
     25944,
     9730
    ],
    [
     "acct_003",
     "2026-07-14",
     25475,
     15972,
     9503
    ],
    [
     "acct_012",
     "2026-07-28",
     47479,
     38105,
     9374
    ],
    [
     "acct_003",
     "2026-07-25",
     23327,
     14114,
     9213
    ],
    [
     "acct_015",
     "2026-07-15",
     33381,
     24237,
     9144
    ],
    [
     "acct_012",
     "2026-07-29",
     38105,
     29284,
     8821
    ],
    [
     "acct_015",
     "2026-07-20",
     42709,
     33952,
     8757
    ]
   ],
   "author_reading": "Join each July USD daily balance record to the same account's record exactly one calendar day earlier, requiring the earlier date also to be in July. Rank positive closing-balance declines with account and later date breaking ties.",
   "author_sql": "SELECT d.account_id, strftime(d.balance_date, '%Y-%m-%d') AS later_date, p.closing_minor AS previous_closing_cents, d.closing_minor AS later_closing_cents, p.closing_minor - d.closing_minor AS decline_cents FROM account_daily d JOIN account_daily p ON p.account_id = d.account_id AND p.currency = d.currency AND p.balance_date = d.balance_date - INTERVAL '1 day' WHERE d.currency = 'USD' AND d.balance_date >= DATE '2026-07-02' AND d.balance_date < DATE '2026-08-01' AND p.balance_date >= DATE '2026-07-01' AND p.closing_minor > d.closing_minor ORDER BY decline_cents DESC, d.account_id ASC, later_date ASC LIMIT 10"
  }
 ]
}
