cswork / libraryOpen practice workspace

Coding questions · Stripe

Reconcile Monthly Bank-Account Balances Across Two Datasets

Compare bank-account balances for a target calendar month, report match status, and distinguish missing comparison records from zero balances.

Write a PostgreSQL query that compares monthly bank-account balances from two imported datasets and marks each account in the first dataset as matching or not matching the second.

Tables

Both primary_balances and comparison_balances have columns:

bank_name text, account_name text, start_date date, end_date date, balance numeric(18,2)

report_month(month_start date) contains exactly one row whose value is the first day of the target month.

Exercise Conventions

  • A record belongs to the calendar month containing its end_date.
  • In each input table, there is at most one record per bank/account/end-date month. All fields are non-null, and start_date <= end_date.
  • Report each primary record in the target month. Accounts appearing only in the comparison table are outside this report.
  • Match records by bank name, account name, and target month, even if the two end dates within that month differ.
  • A primary record is Match only when a comparison record exists and the two numeric balances are equal. Otherwise it is Not Match.
  • Do not sum records across years or treat a missing comparison record as a zero balance.

These schema and missing-record conventions make the monthly reconciliation task precise.

Output

Return bank_name, account_name, end_date, balance, and match_status. The end date and balance come from the primary record. Sort by bank name and then account name using COLLATE "C" for deterministic text order.

Example

Target month: 2021-12-01.

primary_balances:

bank_nameaccount_namestart_dateend_datebalance
Bay022021-12-012021-12-31300.00
Harbor012021-12-012021-12-31150.00
Harbor012020-12-012020-12-31999.00

comparison_balances:

bank_nameaccount_namestart_dateend_datebalance
Bay022021-12-012021-12-31250.00
Harbor012021-12-012021-12-30150.00

Expected output:

bank_nameaccount_nameend_datebalancematch_status
Bay022021-12-31300.00Not Match
Harbor012021-12-31150.00Match

Monthly Bank Balance Reconciliation

Table primary_balances

columntypeconstraints
bank_nameTEXTNOT NULL
account_nameTEXTNOT NULL
start_dateDATENOT NULL
end_dateDATENOT NULL
balanceNUMERIC(18,2)NOT NULL

Sample data:

account_namebalancebank_nameend_datestart_date
02300Bay2021-12-312021-12-01
01150Harbor2021-12-312021-12-01
01999Harbor2020-12-312020-12-01
020Harbor2021-12-312021-12-01
0275bay2021-12-012021-12-01
01120.5Bay2021-12-312021-12-01
0380Harbor2021-12-312021-12-01
1045.251st Credit2021-12-312021-12-01
9601st Credit2021-12-312021-12-01
01500Summit2021-11-302021-11-01
0120Cove2022-01-012022-01-01

Table comparison_balances

columntypeconstraints
bank_nameTEXTNOT NULL
account_nameTEXTNOT NULL
start_dateDATENOT NULL
end_dateDATENOT NULL
balanceNUMERIC(18,2)NOT NULL

Sample data:

account_namebalancebank_nameend_datestart_date
02250Bay2021-12-312021-12-01
01150Harbor2021-12-302021-12-01
01999Harbor2020-12-312020-12-01
0275bay2021-12-312021-12-01
01120.5Bay2021-11-302021-11-01
0350Harbor2021-12-312021-12-01
0330Harbor2020-12-312020-12-01
1045.251st Credit2021-12-312021-12-01
01500Summit2021-12-312021-12-01
0120Cove2022-01-312022-01-01

Table report_month

columntypeconstraints
month_startDATEPRIMARY KEY

Sample data:

month_start
2021-12-01

Expected output

bank_nameaccount_nameend_datebalancematch_status
1st Credit102021-12-3145.25Match
1st Credit92021-12-3160Not Match
Bay012021-12-31120.5Not Match
Bay022021-12-31300Not Match
Harbor012021-12-31150Match
Harbor022021-12-310Not Match
Harbor032021-12-3180Not Match
bay022021-12-0175Match

Hints

  1. Use the single report_month row to decide which calendar month each end_date must fall in.
  2. Every target-month primary record must appear in the output, whether or not a comparison record exists for it.
  3. Two records describe the same account-month when bank_name, account_name and the month of end_date agree; the exact end dates may differ.
  4. A missing comparison record is not the same as a comparison balance of zero.