leetcode 1555 银行账号概要(postgresql)
创始人
2024-11-19 05:33:53
0

需求

用户表: Users

±-------------±--------+
| Column Name | Type |
±-------------±--------+
| user_id | int |
| user_name | varchar |
| credit | int |
±-------------±--------+
user_id 是这个表的主键。
表中的每一列包含每一个用户当前的额度信息。

交易表:Transactions

±--------------±--------+
| Column Name | Type |
±--------------±--------+
| trans_id | int |
| paid_by | int |
| paid_to | int |
| amount | int |
| transacted_on | date |
±--------------±--------+
trans_id 是这个表的主键。
表中的每一列包含银行的交易信息。
ID 为 paid_by 的用户给 ID 为 paid_to 的用户转账。

力扣银行 (LCB) 帮助程序员们完成虚拟支付。我们的银行在表 Transaction 中记录每条交易信息,我们要查询每个用户的当前余额,并检查他们是否已透支(当前额度小于 0)。

写一条 SQL 语句,查询:

user_id 用户 ID
user_name 用户名
credit 完成交易后的余额
credit_limit_breached 检查是否透支 (“Yes” 或 “No”)
以任意顺序返回结果表。

查询格式见如下所示。

示例 1:

输入:
Users 表:
±-----------±-------------±------------+
| user_id | user_name | credit |
±-----------±-------------±------------+
| 1 | Moustafa | 100 |
| 2 | Jonathan | 200 |
| 3 | Winston | 10000 |
| 4 | Luis | 800 |
±-----------±-------------±------------+

Transactions 表:
±-----------±-----------±-----------±---------±--------------+
| 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 |
±-----------±-----------±-----------±---------±--------------+

输出:
±-----------±-----------±-----------±----------------------+
| 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 在 “2020-08-01” 支付了 $400 并在 “2020-08-03” 收到了 $200 ,当前额度 (100 -400 +200) = -$100
Jonathan 在 “2020-08-02” 收到了 $500 并在 “2020-08-08” 支付了 $200 ,当前额度 (200 +500 -200) = $500
Winston 在 “2020-08-01” 收到了 $400 并在 “2020-08-03” 支付了 $500 ,当前额度 (10000 +400 -500) = $9900
Luis 未收到任何转账信息,额度 = $800

输入

在这里插入图片描述

输出

with t1 as ( select user_id,user_name,coalesce(credit-t.amount,credit) as credit1 from users u left join transactions t on user_id=paid_by ),t2 as ( select user_id,user_name,coalesce(credit1+t2.amount,credit1) as credit2 from t1 left join transactions t2 on t1.user_id=t2.paid_to ) select user_id,user_name,credit2 as credit,        case when credit2>=0 then 'No' else 'Yes' end as credit_limit_breached from t2 

在这里插入图片描述

相关内容

热门资讯

WPK透视辅助!德州局脚本,w... WPK透视辅助!德州局脚本,wpk德州局透视(切实是有挂)1、超多福利:超高返利,海量正版游戏,wp...
微扑克透视辅助!pokemmo... 微扑克透视辅助!pokemmo辅助器手机版下载,hhpoker开挂教程(一贯是真的有挂)1、微扑克透...
WPK透视辅助!扑克之星辅助,... WPK透视辅助!扑克之星辅助,wepoker辅助是真的假的(真是真的有挂)wepoker辅助是真的假...
wpk透视辅助!hhpoker... wpk透视辅助!hhpoker是内部控制吗,wepoker破解工具(好像真的有挂)1、hhpoker...
微扑克透视辅助!hhpoker... 微扑克透视辅助!hhpoker德州透视,xpoker透视辅助(好像真的是有挂)1)hhpoker德州...
微扑克透视辅助!黑侠破解wep... 微扑克透视辅助!黑侠破解wepoker,wepoker辅助分析器(切实是有挂)1、首先打开wepok...
wpk透视辅助!aapoker... wpk透视辅助!aapoker怎么设置抽水,大菠萝手游辅助(总是真的有挂)运aapoker怎么设置抽...
wpk透视辅助!wepoker... wpk透视辅助!wepoker软件辅助程序,wepoker辅助器安装包定制(其实是有挂)小薇(辅助器...
微扑克透视辅助!aapoker... 微扑克透视辅助!aapoker怎么提高中牌率,hhpoker软件安装包(确实真的有挂)1、进入游戏-...
WPK透视辅助!wepoker... 您好,wepoker透视器免费这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...