bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1555. Bank Account Summary

1555. Bank Account Summary

Leetcode Solutions

Calculating User Balances with Transaction Adjustments

  1. Create a CTE named cte that selects paid_by as user_id and -amount as amount from Transactions to represent money going out.
  2. Union the above with another select that takes paid_to as user_id and amount as is to represent money coming in.
  3. Sum the amounts in the CTE grouped by user_id to get the net transaction amount for each user.
  4. Left join the Users table with the CTE to combine user information with their transaction summary.
  5. Calculate the current credit for each user by adding their initial credit to their net transaction amount.
  6. Use a CASE statement to determine if the credit limit is breached by checking if the current credit is less than 0.
  7. Select the required columns: user_id, user_name, credit, and credit_limit_breached.

erDiagram
    Users {
        int user_id PK
        varchar user_name
        int credit
    }
    Transactions {
        int trans_id PK
        int paid_by FK
        int paid_to FK
        int amount
        date transacted_on
    }

Aggregate Transaction Totals and Update User Credits

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...