KatInterview

Monthly USD revenue pivot with as-of FX rate lookups

SQL & DataModel answer
MediumModel answerSQL & Datasqlas-of-joinpivot

Last updated

Convert multi-currency sales to USD with an as-of rate join, pivot revenue by category and region, find August's top customer per region, and flag missing FX rates.

You are building a revenue report for a finance team. Sales are recorded in local currencies, and the report must be in USD using the exchange rate that was in effect on each sale date.

Tables

transactions

txn_idtxn_datecustomer_idproduct_idregioncurrencyamount
12025-08-28C1P1NAUSD1200
22025-08-30C2P2EUEUR900
32025-07-15C3P1APACJPY150000
42025-06-20C1P3NAUSD500
52025-08-05C3P2EUEUR700
62025-07-31C2P3NAUSD300

fx_rates (rate_to_usd is the number of USD for one unit of the currency)

currencyrate_daterate_to_usd
EUR2025-08-291.10
EUR2025-07-311.12
JPY2025-07-100.0065
JPY2025-06-300.0068

products

product_idproduct_namecategory
P1AlphaSaaS
P2BetaServices
P3GammaHardware

customers

customer_idnamesegment
C1AcmeEnterprise
C2GlobexMid-Market
C3InitechSMB

Tasks

A. USD conversion. For every transaction, find the most recent FX rate dated on or before txn_date, and convert the amount to USD. A rate older than 30 days before the transaction counts as missing. USD rows need no lookup. Return txn_id, txn_date, customer_id, product_id, region, category, segment, amount_usd, fx_missing_flag. Set the flag to 1 when a non-USD transaction has no usable rate, and 0 otherwise. No transaction may be dropped or duplicated.

B. Pivot report. For June through August 2025, return one row per month and product category, with one column per region (NA, EU, APAC) and a Total column, all summing amount_usd. Derive the month from txn_date rather than hardcoding month values.

C. Top customer per region. For August 2025 only, return the one customer with the highest total amount_usd in each region. Break ties first by the highest single-transaction amount_usd in that month, then by customer_id ascending.

D. Data-quality check. List the non-USD transactions that have no FX rate on or before txn_date within the prior 30 days. These must be exactly the rows where fx_missing_flag = 1. In one sentence, explain how you would monitor this in production.

State which SQL dialect you assume, and explain how your queries handle missing rates, empty groups and ties.

Hints

Hint 1

For an as-of lookup, join every rate of the same currency with rate_date <= txn_date (and within 30 days), then use ROW_NUMBER() ordered by rate_date DESC and keep row 1. Put that rn = 1 test in the LEFT JOIN's ON clause, not in WHERE.

Hint 2

A pivot is conditional aggregation: SUM(amount_usd) FILTER (WHERE region = 'EU') or SUM(CASE WHEN ... END) per column, grouped by the month from DATE_TRUNC and the category. Total is the plain SUM.

Hint 3

For the top customer, first aggregate to (region, customer) with SUM and MAX, then rank with ROW_NUMBER() PARTITION BY region ORDER BY total DESC, max_txn DESC, customer_id ASC. Expect no APAC row for August.