SQL & Queries


Answer & solution
Answer: Self-join by equal account and T2.date<=T1.date, without DISTINCT. (no official key was published)
One SQL definition is
CREATE VIEW TP (acctno, txn_date, amount) AS SELECT T1.acctno, T1.date, T2.amount FROM transaction AS T1 JOIN transaction AS T2 ON T1.acctno = T2.acctno AND T2.date <= T1.date;
Here T1 chooses a balance-observation date and T2 chooses one transaction contributing to that balance. Include equality so the transaction on the observation date contributes; using < would produce the balance before, rather than after, that transaction and omit the first date’s self-pair. Each qualifying transaction pair contributes a row. Do not add DISTINCT: SQL’s bag semantics deliberately preserve separate same-amount transactions even though the projected triples may be identical. For example two deposits of 100 on different days generate two copies of (account,later_date,100), both of which must be summed. transaction and date can be reserved identifiers in some SQL dialects; quote the existing identifiers with that dialect’s supported delimiters if needed.