高级农民
- 积分
- 4176
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
1555. Bank Account Summary
Medium
Topics
SQL Schema
Pandas Schema
Table: Users
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| user_id | int |
| user_name | varchar |
| credit | int |
+--------------+---------+
user_id is the primary key (column with unique values) 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 (column with unique values) for this table.
Each row of this table contains 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 whether they have breached their credit limit (If their current credit is less than 0).
Write a solution to report.
user_id,
user_name,
credit, current balance after performing transactions, and
credit_limit_breached, check credit_limit ("Yes" or "No")
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
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 |
+------------+------------+------------+----------+---------------+
Output:
+------------+------------+------------+-----------------------+
| user_id | user_name | credit | credit_limit_breached |
+------------+------------+------------+-----------------------+
| 1 | Moustafa | -100 | Yes |
| 2 | Jonathan | 500 | No |
| 3 | Winston | 9900 | No |
| 4 | Luis | 800 | No |
+------------+------------+------------+-----------------------+
Explanation:
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 did not received any transfer, credit = $800
思路
每个用户的最终余额 = 初始额度 − 转出总额 + 转入总额。
转出:用户作为 paid_by 时,余额减少 amount。
转入:用户作为 paid_to 时,余额增加 amount。
没有交易的用户(如 Luis)也要输出,余额就是初始额度,所以以 Users 为主表做 LEFT JOIN。
是否超限:余额 < 0 为 Yes,否则为 No。余额恰好为 0 不算超限。- # Write your MySQL query statement below
- WITH flow AS (
- SELECT paid_by AS user_id, -amount AS change_amt FROM Transactions
- UNION ALL
- SELECT paid_to AS user_id, amount AS change_amt FROM Transactions
- )
- SELECT
- user_id,
- user_name,
- credit,
- IF(credit < 0, 'Yes', 'No') AS credit_limit_breached
- FROM (
- SELECT
- u.user_id,
- u.user_name,
- u.credit + IFNULL(SUM(f.change_amt), 0) AS credit
- FROM Users u
- LEFT JOIN flow f
- ON u.user_id = f.user_id
- GROUP BY u.user_id, u.user_name, u.credit
- ) t;
复制代码 |
|