Write a PostgreSQL query that returns the latest reported balance for one bank account.
Tables
bank_balances(bank_name text, account_name text, start_date date, end_date date, balance numeric(18,2))
requested_account(bank_name text, account_name text) contains exactly one row.
Rules
-
Match both bank name and account name exactly. Account names are text identifiers and may contain leading zeros.
-
For this exercise, latest means greatest
end_date
; each bank/account pair has at most one row with a given end date.
-
Every field is non-null, and
start_date <= end_date
.
-
Records may span different months and years. Dates are typed dates, not strings to compare lexicographically.
Output
Return one column named balance for the latest matching record. If the requested account has no record, return no rows. The end-date definition and missing-account behavior are explicit exercise conventions.
Example
bank_balances:
| bank_name | account_name | start_date | end_date | balance |
|---|
| Harbor | 001234 | 2021-12-01 | 2021-12-31 | 150.00 |
| Harbor | 001234 | 2019-05-01 | 2019-05-31 | 200.00 |
| Bay | 001234 | 2022-01-01 | 2022-01-31 | 300.00 |
requested_account: ('Harbor', '001234')
Expected output: