Bank Account Summary Problem
Description
LeetCode Problem 1555.
Table: Users
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| user_id | int |
| user_name | varchar |
| credit | int |
+--------------+---------+
user_id is the primary key for this table.
Each row of this table contains the current credit information for each user.
Table: Transactions
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| trans_id | int |
| paid_by | int |
| paid_to | int |
| amount | int |
| transacted_on | date |
+---------------+---------+
trans_id is the primary key for this table.
Each row of this table contains the information about the transaction in the bank.
User with id (paid_by) transfer money to user with id (paid_to).
Leetcode Bank (LCB) helps its coders in making virtual payments. Our bank records all transactions in the table Transaction, we want to find out the current balance of all users and check wheter they have breached their credit limit (If their current credit is less than 0).
Write an SQL query to report.
- user_id
- user_name
- credit, current balance after performing transactions.
- credit_limit_breached, check credit_limit ("Yes" or "No")
Return the result table in any order.
The query result format is in the following example.
Users table:
+------------+--------------+-------------+
| user_id | user_name | credit |
+------------+--------------+-------------+
| 1 | Moustafa | 100 |
| 2 | Jonathan | 200 |
| 3 | Winston | 10000 |
| 4 | Luis | 800 |
+------------+--------------+-------------+
Transactions table:
+------------+------------+------------+----------+---------------+
| trans_id | paid_by | paid_to | amount | transacted_on |
+------------+------------+------------+----------+---------------+
| 1 | 1 | 3 | 400 | 2020-08-01 |
| 2 | 3 | 2 | 500 | 2020-08-02 |
| 3 | 2 | 1 | 200 | 2020-08-03 |
+------------+------------+------------+----------+---------------+
Result table:
+------------+------------+------------+-----------------------+
| user_id | user_name | credit | credit_limit_breached |
+------------+------------+------------+-----------------------+
| 1 | Moustafa | -100 | Yes |
| 2 | Jonathan | 500 | No |
| 3 | Winston | 9900 | No |
| 4 | Luis | 800 | No |
+------------+------------+------------+-----------------------+
Moustafa paid $400 on "2020-08-01" and received $200 on "2020-08-03", credit (100 -400 +200) = -$100
Jonathan received $500 on "2020-08-02" and paid $200 on "2020-08-08", credit (200 +500 -200) = $500
Winston received $400 on "2020-08-01" and paid $500 on "2020-08-03", credit (10000 +400 -500) = $9990
Luis didn't received any transfer, credit = $800
Solution
Approach: UNION ALL the Money Movements, then LEFT JOIN
A transaction changes two balances: the payer (paid_by) loses amount and the receiver (paid_to) gains it. So turn each transaction into two rows of (user_id, change):
(paid_by, -amount)(paid_to, +amount)
UNION ALL matters here. Plain UNION would merge two identical movements, for example two equal payments by the same user, and the balance would be wrong.
Then join Users to these changes with a LEFT JOIN, so a user with no transactions (Luis) still appears. For such a user SUM returns NULL, and COALESCE(..., 0) turns that into no change, so their balance is just their starting credit.
The last step compares the new balance with 0: below 0 means credit_limit_breached is 'Yes', otherwise 'No'.
The Pandas version sums what each user paid and received with two groupbys, maps both onto users (missing means 0), and derives the 'Yes'/'No' column from the new credit.
WITH balance_changes AS (
-- The payer loses the amount, the receiver gains it.
SELECT paid_by AS user_id, -amount AS amount
FROM Transactions
UNION ALL
SELECT paid_to AS user_id, amount
FROM Transactions
),
balances AS (
SELECT u.user_id,
u.user_name,
u.credit + COALESCE(SUM(b.amount), 0) AS credit
FROM Users AS u
LEFT JOIN balance_changes AS b
ON b.user_id = u.user_id
GROUP BY u.user_id, u.user_name, u.credit
)
SELECT user_id,
user_name,
credit,
CASE WHEN credit < 0 THEN 'Yes' ELSE 'No' END AS credit_limit_breached
FROM balances;