LeetCode_sql_day17(1843.可疑银行账户)

devtools/2024/12/22 19:26:25/

描述:

表:Accounts
+----------------+------+
| Column Name    | Type |
+----------------+------+
| account_id     | int  |
| max_income     | int  |
+----------------+------+
account_id 是这张表具有唯一值的列。
每行包含一个银行账户每月最大收入的信息。

表:Transactions

+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| transaction_id | int      |
| account_id     | int      |
| type           | ENUM     |
| amount         | int      |
| day            | datetime |
+----------------+----------+
transaction_id 是这张表具有唯一值的列。
每行包含一条转账信息。
type 是枚举类型(包含'Creditor','Debtor'),其中 'Creditor' 表示用户向其账户存入资金,'Debtor' 表示用户从其账户取出资金。
amount 是交易过程中的存入/取出的金额。

如果一个账户在 连续两个及以上 月份的 总收入 超过最大收入(max_income),那么认为这个账户 可疑。  账户当月 总收入 是当月存入资金总数(即 transactions 表中 type 字段的 'Creditor')。

编写一个解决方案,报告所有的 可疑 账户。

以 任意顺序 返回结果表

返回结果格式如下示例所示。

示例 1:

输入:
Accounts 表:
+------------+------------+
| account_id | max_income |
+------------+------------+
| 3          | 21000      |
| 4          | 10400      |
+------------+------------+
Transactions 表:
+----------------+------------+----------+--------+---------------------+
| transaction_id | account_id | type     | amount | day                 |
+----------------+------------+----------+--------+---------------------+
| 2              | 3          | Creditor | 107100 | 2021-06-02 11:38:14 |
| 4              | 4          | Creditor | 10400  | 2021-06-20 12:39:18 |
| 11             | 4          | Debtor   | 58800  | 2021-07-23 12:41:55 |
| 1              | 4          | Creditor | 49300  | 2021-05-03 16:11:04 |
| 15             | 3          | Debtor   | 75500  | 2021-05-23 14:40:20 |
| 10             | 3          | Creditor | 102100 | 2021-06-15 10:37:16 |
| 14             | 4          | Creditor | 56300  | 2021-07-21 12:12:25 |
| 19             | 4          | Debtor   | 101100 | 2021-05-09 15:21:49 |
| 8              | 3          | Creditor | 64900  | 2021-07-26 15:09:56 |
| 7              | 3          | Creditor | 90900  | 2021-06-14 11:23:07 |
+----------------+------------+----------+--------+---------------------+
输出:
+------------+
| account_id |
+------------+
| 3          |
+------------+
解释:
对于账户 3:
- 在 2021年6月,用户收入为 107100 + 102100 + 90900 = 300100。
- 在 2021年7月,用户收入为 64900。
可见收入连续两月超过21000的最大收入,因此账户3列入结果表中。对于账户 4:
- 在 2021年5月,用户收入为 49300。
- 在 2021年6月,用户收入为 10400。
- 在 2021年7月,用户收入为 56300。
可见收入在5月与7月超过了最大收入,但6月没有。因为账户没有没有连续两月超过最大收入,账户4不列入结果表中。

数据准备:

Create table If Not Exists Accounts (account_id int, max_income int)

Create table If Not Exists Transactions (transaction_id int, account_id int, type ENUM('creditor', 'debtor'), amount int, day datetime)

Truncate table Accounts

insert into Accounts (account_id, max_income) values ('3', '21000')

insert into Accounts (account_id, max_income) values ('4', '10400')

Truncate table Transactions

insert into Transactions (transaction_id, account_id, type, amount, day) values ('2', '3', 'Creditor', '107100', '2021-06-02 11:38:14')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('4', '4', 'Creditor', '10400', '2021-06-20 12:39:18')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('11', '4', 'Debtor', '58800', '2021-07-23 12:41:55')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('1', '4', 'Creditor', '49300', '2021-05-03 16:11:04')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('15', '3', 'Debtor', '75500', '2021-05-23 14:40:20')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('10', '3', 'Creditor', '102100', '2021-06-15 10:37:16')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('14', '4', 'Creditor', '56300', '2021-07-21 12:12:25')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('19', '4', 'Debtor', '101100', '2021-05-09 15:21:49')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('8', '3', 'Creditor', '64900', '2021-07-26 15:09:56')

insert into Transactions (transaction_id, account_id, type, amount, day) values ('7', '3', 'Creditor', '90900', '2021-06-14 11:23:07')

分析:

①首先先将type为creditor的数据筛选出来,同时将日期拆分为年、月

selectaccount_id,amount,year(day)as year,substr(day,6,2)as month
from transactions where type = 'creditor'
order by account_id,year,month 

②将每个account_id对应的max_income关联起来 然后过滤 根据账户id、年月分组排名

with t1 as (
selectaccount_id,amount,year(day)as year,substr(day,6,2)as month
from transactions where type = 'creditor'
order by account_id,year,month )
, t2 as(
select t1.account_id,sum(amount)total,year,month,max_income from t1,accountswhere t1.account_id = Accounts.account_id
group by account_id, year, month,max_income
order by account_id)select account_id,total,year,month,max_income,row_number() over (partition by account_id order by month)r1
from t2
where total > max_income

③用month-r1 如果数值相同 那么就是连续月份,同时求出相同数值的个数 并且过滤出大于等于2的即为连续两个月及以上

with t1 as (
selectaccount_id,amount,year(day)as year,substr(day,6,2)as month
from transactions where type = 'creditor'
order by account_id,year,month )
, t2 as(
select t1.account_id,sum(amount)total,year,month,max_income from t1,accountswhere t1.account_id = Accounts.account_id
group by account_id, year, month,max_income
order by account_id)
, t3 as (
select account_id,total,year,month,max_income,row_number() over (partition by account_id order by month)r1
from t2
where total > max_income)
# , t4 as (
select account_id,year,(month-r1) as m2,count((month-r1)) as r3
from t3
group by account_id ,year,(month-r1)
having (count((month-r1))) >=2

图解:

代码:

sql">with t1 as (
selectaccount_id,amount,year(day)as year,substr(day,6,2)as month
from transactions where type = 'creditor'
order by account_id,year,month )
, t2 as(
select t1.account_id,sum(amount)total,year,month,max_income from t1,accountswhere t1.account_id = Accounts.account_id
group by account_id, year, month,max_income
order by account_id)
, t3 as (
select account_id,total,year,month,max_income,row_number() over (partition by account_id order by month)r1
from t2
where total > max_income)
, t4 as (
select account_id,year,(month-r1) as m2,count((month-r1)) as r3
from t3
group by account_id ,year,(month-r1))select distinct account_id from t4where r3 >=2;

总结:

将日拆解为年和月 并通过年月分组是关键 

同时注意用月份减去排名值相同的即为连续


http://www.ppmy.cn/devtools/103262.html

相关文章

【离线查询 滑动窗口】2747. 统计没有收到请求的服务器数目

本文涉及知识点 离线查询 C算法:滑动窗口总结 LeetCode2747. 统计没有收到请求的服务器数目 给你一个整数 n ,表示服务器的总数目,再给你一个下标从 0 开始的 二维 整数数组 logs ,其中 logs[i] [server_id, time] 表示 id 为…

【ceph学习】ceph如何进行数据的读写(1)

版本 ceph版本为17. ceph如何进行读写接口的实现 Ceph的客户端通过librados的接口进行集群的访问,这里的访问包括: 1)对集群的整体访问 2)对象的访问 两类接口,这套接口(API)包括C、C和Pytho…

Ruby 多线程

Ruby 多线程 在当今的软件开发领域,多线程已经成为提高程序性能和响应速度的关键技术之一。Ruby,作为一种现代的编程语言,提供了丰富的多线程支持,使得开发者能够轻松地构建高效、并发的应用程序。本文将深入探讨Ruby中的多线程概念、用法以及最佳实践。 什么是多线程? …

图像搜索引擎DIY【CLIP+FAISS】

你是否曾经想在无穷无尽的图像数据集中查找图像,但发现这太过繁琐?在本教程中,我们将构建一个图像相似性搜索引擎,以便使用文本查询或参考图像轻松查找图像。为方便起见,本文底部以 Colab 笔记本的形式提供了本教程的完…

PyAutoGui的使用

文章目录 一、屏幕1.获取屏幕分辨率2.查看指定位置的像素点是否在屏幕上 二、鼠标1.获取鼠标位置2.控制鼠标运动3.鼠标拖动4.鼠标点击5.鼠标的按压与释放6.鼠标滚动 三、键盘1.控制键盘2.按下后释放一个键3.按顺序按下键,然后反向顺序释放 四、图像1.截图2.获取图像…

【TPAMI 2024】Occlusion-Aware Self-Supervised Monocular 6D Object Pose Estimation

TPAMI 2024 | 我3D都没搞明白,这都开始6D了!?而且这个单目6D物体姿态估计技术,让机器视觉无需人类教导! Occlusion-Aware Self-Supervised Monocular 6D Object Pose Estimation 题目:遮挡感知的自监督单…

音视频-图像篇(YUV和RGB)

文章目录 一、图像基础概念二、YUV与RGB1.YUV分类方式2.YUV“空间-间”的数据划分1)UV按照“空间-间”的划分方式,分为YUV444、YUV422、YUV4202)YUV“空间-内”的数据划分 3.RGB 三、比较JPG、PNG、GIF、BMP图片格式 一、图像基础概念 像素&…

(自用)适时小结(一)

前言 每过一段时间,总结一下学习方面的感悟,可能和编程有关,可能和学习方法有关,也可能对前面学过东西的回顾,或者单纯表达一些想法. 关于C C的学习至少要经过两个阶段:基础学习和熟练掌握.常见的问题:学习的过程中会产生一些迷茫,C到底能干什么? .C的内容不少,学习难度也不低…