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

;

总结:

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

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

相关推荐
goTsHgo28 分钟前
clickhouse适用的业务场景
数据库·clickhouse
老华带你飞38 分钟前
美术|基于java+vue的美术外包管理信息系统(源码+数据库+文档)
java·数据库·vue.js
懂一点的陈老师38 分钟前
redis分布式锁死锁场景
数据库·分布式·死锁
MARSERERER1 小时前
JDBC客户端连接Starrocks 2.5
数据库
创作小达人1 小时前
医药|基于springboot的医药管理系统设计与实现(附项目源码+论文+数据库)
数据库·spring boot·后端
NeVeRMoRE_20241 小时前
【数据结构和算法实践-树-LeetCode110-平衡二叉树】
数据结构·b树·算法·leetcode
一只邪恶大泡2 小时前
力扣14.最长公共前缀
数据结构·算法·leetcode
威震四海3 小时前
mysql数据库如何开启binlog日志
数据库·mysql
&木头人&3 小时前
oracle 用游标为什么会比for循环慢?
数据库·oracle
星空寻流年3 小时前
Spring security 动态权限管理(基于数据库)
数据库·mysql·spring