+-------------+------+
|Column Name |Type|+-------------+------+
| account_id | int ||day| date ||type| ENUM || amount | int |+-------------+------+
(account_id, day) is the primarykeyfor this table.
Eachrowcontains information about one transaction, including the transactiontype, the day it occurred on, and the amount.
typeis an ENUM of the type ('Deposit','Withdraw')
Write an SQL query to report the balance of each user after each transaction. You may assume that the balance of each account before any transaction is 0 and that the balance will never be below 0 at any moment.
Return the result table in ascending order by account_id, then by day in case of a tie.
The query result format is in the following example.
The key idea is to compute the running balance for each account after every transaction. We can use SQL window functions to calculate a cumulative sum, adding for deposits and subtracting for withdrawals, ordered by transaction date.