SQL & Queries



Answer & solution
Answer: V sums amounts by account and observation date; final query takes MIN(balance) by account. (no official key was published)
Use two aggregation levels:
CREATE VIEW V (acctno, txn_date, balance) AS
SELECT acctno, txn_date, SUM(amount)
FROM TP
GROUP BY acctno, txn_date;
SELECT acctno, MIN(balance) AS minbalance
FROM V
GROUP BY acctno;
For a fixed account and T1 date, TP has one amount row for every transaction up to and including that date. SUM therefore gives the cumulative balance immediately after the transaction, assuming the opening balance is zero as specified. The one-transaction-per-account-per-day condition ensures each observation date has one T1 row, avoiding repeated copies of the entire prefix caused by multiple T1 transactions on the same day. Taking MIN over these cumulative balances returns the lowest post-transaction balance, including negative balances when appropriate. There is no row for account creation, so the initial zero is not accidentally included; a positive-only account correctly returns its first positive balance rather than zero. An account with amounts 100,100,−250 has balances 100,200,−50 and minimum−50. DISTINCT in TP would collapse the two 100 entries at the later dates and corrupt these sums. Distinct dates, not distinct amounts, define observation groups. Non-null numeric amounts and a comparable chronological date type are implicit in the bank model.