Chapter 8 · Window Functions
42. Detect Three Consecutive Deposits
Hard40 XPFinance
Find accounts that have three or more consecutive deposit transactions (with no other transaction type in between, ordered by transaction id). Return the account_id and the count of such consecutive deposit streaks (streak_count). Use LAG or LEAD window functions to detect consecutive types. Order by streak_count descending, then account_id ascending.
Tables
- PKidINT
- FKaccount_id→ accounts.idINT
- transaction_dateTEXT
- typeTEXT
- amountREAL
- balance_afterREAL
- descriptionTEXT
- reference_numberTEXT
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.