LeetCode_sql_day17(1843.可疑银行账户)

描述:

复制代码
表: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的数据筛选出来,同时将日期拆分为年、月

复制代码
select
    account_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 (
select
    account_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,accounts
                                                            where 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 (
select
    account_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,accounts
                                                            where 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 (
select
    account_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,accounts
                                                            where 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 t4
    where r3 >=2

;

总结:

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

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

相关推荐
Maybyy1 小时前
力扣61.旋转链表
算法·leetcode·链表
sun0077001 小时前
mysql索引底层原理
数据库·mysql
chao_7894 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
workflower4 小时前
MDSE和敏捷开发相互矛盾之处:方法论本质的冲突
数据库·软件工程·敏捷流程·极限编程
Tony小周4 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lifallen5 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
TDengine (老段)5 小时前
TDengine 数据库建模最佳实践
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Elastic 中国社区官方博客5 小时前
Elasticsearch 字符串包含子字符串:高级查询技巧
大数据·数据库·elasticsearch·搜索引擎·全文检索·lucene
Gauss松鼠会6 小时前
GaussDB应用场景全景解析:从金融核心到物联网的分布式数据库实践
数据库·分布式·物联网·金融·database·gaussdb
守城小轩6 小时前
Chromium 136 编译指南 - Android 篇:开发工具安装(三)
android·数据库·redis