Leetcode Problem 2066. Account Balance

2066. Account Balance

Leetcode Solutions

Using Window Function for Cumulative Sum

  1. Select the account_id and day columns from the Transactions table.
  2. Use a CASE statement to determine the signed amount of each transaction (positive for 'Deposit', negative for 'Withdraw').
  3. Use the SUM window function to calculate the running total of the signed amounts, partitioned by account_id and ordered by day.
  4. Alias the running total as balance.
  5. Order the final result by account_id and day.

erDiagram
    Transactions {
        int account_id
        date day
        ENUM type
        int amount
        account_id_day PK
    }

Self-Join with Aggregation for Cumulative Sum

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...