Daily Net Cash Flow and Running Balance per Wealth Client Account
Last updated: November 29, 2026
Quick Overview
Given a table of cash movements (account, timestamp, debit or credit direction, amount, booking status), write SQL that returns each account's net flow per booking day and its running balance over time, counting only booked movements.
UBS
November 29, 20260
0
0 solved
Given a table of cash movements (account, timestamp, debit or credit direction, amount, booking status), write SQL that returns each account's net flow per booking day and its running balance over time, counting only booked movements.
A typical SQL screen for a data engineering role supporting Global Wealth Management reporting, where advisors and client statements need consistent daily cash positions. The interviewer is checking conditional aggregation, window functions with an explicit frame, and whether you think about reversed or pending bookings before writing code.
What the Interviewer Expects
- Ask how debits and credits are encoded and which statuses count
- Aggregate to one row per account per day before windowing
- Use a window SUM partitioned by account and ordered by date
- State the window frame explicitly rather than relying on defaults
- Discuss opening balances and days with no movement
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you include an opening balance carried from a monthly snapshot table?
- How would you produce a row for every calendar day, including days with no movements?
- Accounts hold several currencies: how does the query change?
- This table has billions of rows: how would you partition and incrementally maintain the result?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Table cash_movements(account_id, booked_at TIMESTAMP, direction 'CR'|'DR', amount NUMERIC, status). Amounts are positive and the sign comes from direction. Only status = 'BOOKED' counts; `PENDIN...
Approach
Two steps. First a CTE collapses movements to one row per account and booking date with a signed SUM(CASE ...). Then a window SUM over that CTE, partitioned by account and ordered by date, produce...