Merchant settlement balance per currency in USD with as-of exchange rates

Quick Overview

Write a PostgreSQL query that reports each merchant's pending settlement balance per currency, converted to USD with the exchange rate in effect on each transaction date. It tests as-of rate lookups, date range filtering against settlement periods and conditional aggregation without double counting.

Merchant settlement balance per currency in USD with as-of exchange rates

Company: Adyen

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A payments platform pays out merchants by settlement period. Compute each merchant's settlement balance in USD per currency: the money from completed transactions that sit in a period still waiting to be settled. Return `merchant_id`, `merchant_name`, `currency`, `total_balance_usd` (rounded to 2 decimals), `transaction_count`, `gross_payments` and `total_deductions` (both in the original currency). Requirements: - Include only `status = 'COMPLETED'` transactions whose `transaction_date` falls in a settlement period of the same merchant with `settlement_status = 'PENDING'`. Period start and end dates are inclusive. - Convert each transaction using the most recent `rate_to_usd` for its currency with `rate_date` on or before its `transaction_date`. - `PAYMENT` adds to the balance, `REFUND` and `CHARGEBACK` subtract. - `gross_payments` sums `PAYMENT` amounts, `total_deductions` sums `REFUND` and `CHARGEBACK` amounts. ```hint Pin one rate per transaction Resolve the exchange rate for each transaction before you aggregate. Then check that no join in your query can make a transaction appear more than once. ``` ### Input Table(s) `merchants` (primary key `merchant_id`) | Column | Type | Meaning | |---|---|---| | merchant_id | INT | Merchant identifier | | merchant_name | VARCHAR | Merchant display name | `transactions` (primary key `transaction_id`) | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier | | merchant_id | INT | References `merchants.merchant_id` | | amount | DECIMAL(12,2) | Amount in the transaction currency, never negative | | currency | CHAR(3) | ISO currency code of the amount | | transaction_type | VARCHAR | `'PAYMENT'`, `'REFUND'` or `'CHARGEBACK'` | | status | VARCHAR | `'COMPLETED'`, `'PENDING'` or `'FAILED'` | | transaction_date | DATE | Day the transaction happened | `settlements` (primary key `settlement_id`) | Column | Type | Meaning | |---|---|---| | settlement_id | INT | Settlement period identifier | | merchant_id | INT | References `merchants.merchant_id` | | settlement_start_date | DATE | First day of the period, inclusive | | settlement_end_date | DATE | Last day of the period, inclusive | | settlement_status | VARCHAR | `'PENDING'`, `'SETTLED'` or `'CANCELLED'` | `currency_rates` (primary key `(currency, rate_date)`) | Column | Type | Meaning | |---|---|---| | currency | CHAR(3) | ISO currency code | | rate_date | DATE | Day the rate took effect | | rate_to_usd | DECIMAL(12,6) | USD value of one unit of the currency | ### Output Contract - Columns, in this order: `merchant_id`, `merchant_name`, `currency`, `total_balance_usd`, `transaction_count`, `gross_payments`, `total_deductions`. - One row per `(merchant_id, currency)` pair that has at least one qualifying transaction. - `total_balance_usd` is the sum of each qualifying amount times its rate, with refunds and chargebacks negative, rounded to 2 decimals after summing. - `transaction_count` counts the qualifying transactions of all three types. - `gross_payments` and `total_deductions` stay in the original currency, rounded to 2 decimals, and are `0.00` when the group has no transactions of that kind. - Order by `merchant_id` ascending, then `currency` ascending. ### Example `merchants` | merchant_id | merchant_name | |---|---| | 1 | Alpha Shop | | 2 | Beta Goods | `settlements` | settlement_id | merchant_id | settlement_start_date | settlement_end_date | settlement_status | |---|---|---|---|---| | 10 | 1 | 2024-03-01 | 2024-03-15 | PENDING | | 11 | 1 | 2024-02-15 | 2024-02-29 | SETTLED | | 12 | 2 | 2024-03-01 | 2024-03-31 | PENDING | `currency_rates` | currency | rate_date | rate_to_usd | |---|---|---| | EUR | 2024-02-20 | 1.080000 | | EUR | 2024-03-05 | 1.100000 | | GBP | 2024-03-01 | 1.250000 | | USD | 2024-01-01 | 1.000000 | `transactions` | transaction_id | merchant_id | amount | currency | transaction_type | status | transaction_date | |---|---|---|---|---|---|---| | 101 | 1 | 100.00 | EUR | PAYMENT | COMPLETED | 2024-03-02 | | 102 | 1 | 200.00 | EUR | PAYMENT | COMPLETED | 2024-03-06 | | 103 | 1 | 30.00 | EUR | REFUND | COMPLETED | 2024-03-10 | | 104 | 1 | 50.00 | USD | PAYMENT | COMPLETED | 2024-03-03 | | 105 | 1 | 40.00 | EUR | PAYMENT | FAILED | 2024-03-04 | | 106 | 1 | 80.00 | EUR | PAYMENT | COMPLETED | 2024-02-20 | | 107 | 2 | 120.00 | GBP | PAYMENT | COMPLETED | 2024-03-12 | | 108 | 2 | 20.00 | GBP | CHARGEBACK | COMPLETED | 2024-03-20 | | 109 | 2 | 10.00 | GBP | REFUND | PENDING | 2024-03-21 | | 110 | 1 | 60.00 | USD | PAYMENT | COMPLETED | 2024-03-20 | Expected result: | merchant_id | merchant_name | currency | total_balance_usd | transaction_count | gross_payments | total_deductions | |---|---|---|---|---|---|---| | 1 | Alpha Shop | EUR | 295.00 | 3 | 300.00 | 30.00 | | 1 | Alpha Shop | USD | 50.00 | 1 | 50.00 | 0.00 | | 2 | Beta Goods | GBP | 125.00 | 2 | 120.00 | 20.00 | Transaction 101 uses the EUR rate of 2024-02-20 (1.08) because the 2024-03-05 rate came later, so it adds 108.00. Transactions 102 and 103 use 1.10 and add 220.00 and subtract 33.00. Transaction 105 failed, 106 sits in a settled period, 109 is not completed, and 110 falls after merchant 1's pending period ends, so none of them count. ### Constraints and Clarifications - `transactions` has up to 10,000 rows, and every amount is non-negative. - Every currency has at least one rate on or before every transaction date, and a currency has at most one rate per day. - A transaction counts once, even if it falls inside more than one pending period of its merchant. - Transactions that are not completed, that fall only in settled or cancelled periods, or that fall outside every period of their merchant are ignored. - Merchants with no qualifying transactions do not appear in the output. - The balance can be negative when deductions exceed payments. - None of the columns above contain NULLs.

Overview: Write a PostgreSQL query that reports each merchant's pending settlement balance per currency, converted to USD with the exchange rate in effect on each transaction date. It tests as-of rate lookups, date range filtering against settlement periods and conditional aggregation without double counting.

Adyen logo
Adyen
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A payments platform pays out merchants by settlement period. Compute each merchant's settlement balance in USD per currency: the money from completed transactions that sit in a period still waiting to be settled.

Return merchant_id, merchant_name, currency, total_balance_usd (rounded to 2 decimals), transaction_count, gross_payments and total_deductions (both in the original currency).

Requirements:

  • Include only status = 'COMPLETED' transactions whose transaction_date falls in a settlement period of the same merchant with settlement_status = 'PENDING' . Period start and end dates are inclusive.
  • Convert each transaction using the most recent rate_to_usd for its currency with rate_date on or before its transaction_date .
  • PAYMENT adds to the balance, REFUND and CHARGEBACK subtract.
  • gross_payments sums PAYMENT amounts, total_deductions sums REFUND and CHARGEBACK amounts.

Input Table(s)

merchants (primary key merchant_id)

ColumnTypeMeaning
merchant_idINTMerchant identifier
merchant_nameVARCHARMerchant display name

transactions (primary key transaction_id)

ColumnTypeMeaning
transaction_idINTTransaction identifier
merchant_idINTReferences merchants.merchant_id
amountDECIMAL(12,2)Amount in the transaction currency, never negative
currencyCHAR(3)ISO currency code of the amount
transaction_typeVARCHAR'PAYMENT', 'REFUND' or 'CHARGEBACK'
statusVARCHAR'COMPLETED', 'PENDING' or 'FAILED'
transaction_dateDATEDay the transaction happened

settlements (primary key settlement_id)

ColumnTypeMeaning
settlement_idINTSettlement period identifier
merchant_idINTReferences merchants.merchant_id
settlement_start_dateDATEFirst day of the period, inclusive
settlement_end_dateDATELast day of the period, inclusive
settlement_statusVARCHAR'PENDING', 'SETTLED' or 'CANCELLED'

currency_rates (primary key (currency, rate_date))

ColumnTypeMeaning
currencyCHAR(3)ISO currency code
rate_dateDATEDay the rate took effect
rate_to_usdDECIMAL(12,6)USD value of one unit of the currency

Output Contract

  • Columns, in this order: merchant_id , merchant_name , currency , total_balance_usd , transaction_count , gross_payments , total_deductions .
  • One row per (merchant_id, currency) pair that has at least one qualifying transaction.
  • total_balance_usd is the sum of each qualifying amount times its rate, with refunds and chargebacks negative, rounded to 2 decimals after summing.
  • transaction_count counts the qualifying transactions of all three types.
  • gross_payments and total_deductions stay in the original currency, rounded to 2 decimals, and are 0.00 when the group has no transactions of that kind.
  • Order by merchant_id ascending, then currency ascending.

Example

merchants

merchant_idmerchant_name
1Alpha Shop
2Beta Goods

settlements

settlement_idmerchant_idsettlement_start_datesettlement_end_datesettlement_status
1012024-03-012024-03-15PENDING
1112024-02-152024-02-29SETTLED
1222024-03-012024-03-31PENDING

currency_rates

currencyrate_daterate_to_usd
EUR2024-02-201.080000
EUR2024-03-051.100000
GBP2024-03-011.250000
USD2024-01-011.000000

transactions

transaction_idmerchant_idamountcurrencytransaction_typestatustransaction_date
1011100.00EURPAYMENTCOMPLETED2024-03-02
1021200.00EURPAYMENTCOMPLETED2024-03-06
103130.00EURREFUNDCOMPLETED2024-03-10
104150.00USDPAYMENTCOMPLETED2024-03-03
105140.00EURPAYMENTFAILED2024-03-04
106180.00EURPAYMENTCOMPLETED2024-02-20
1072120.00GBPPAYMENTCOMPLETED2024-03-12
108220.00GBPCHARGEBACKCOMPLETED2024-03-20
109210.00GBPREFUNDPENDING2024-03-21
110160.00USDPAYMENTCOMPLETED2024-03-20

Expected result:

merchant_idmerchant_namecurrencytotal_balance_usdtransaction_countgross_paymentstotal_deductions
1Alpha ShopEUR295.003300.0030.00
1Alpha ShopUSD50.00150.000.00
2Beta GoodsGBP125.002120.0020.00

Transaction 101 uses the EUR rate of 2024-02-20 (1.08) because the 2024-03-05 rate came later, so it adds 108.00. Transactions 102 and 103 use 1.10 and add 220.00 and subtract 33.00. Transaction 105 failed, 106 sits in a settled period, 109 is not completed, and 110 falls after merchant 1's pending period ends, so none of them count.

Constraints and Clarifications

  • transactions has up to 10,000 rows, and every amount is non-negative.
  • Every currency has at least one rate on or before every transaction date, and a currency has at most one rate per day.
  • A transaction counts once, even if it falls inside more than one pending period of its merchant.
  • Transactions that are not completed, that fall only in settled or cancelled periods, or that fall outside every period of their merchant are ignored.
  • Merchants with no qualifying transactions do not appear in the output.
  • The balance can be negative when deductions exceed payments.
  • None of the columns above contain NULLs.