MySQL高阶1843-可疑银行账户

目录

题目

准备数据

​分析数据

实现

总结


题目

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

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

准备数据

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');

accounts表

transactions表

 分析数据

第一步:将日期的月提取出来,并且过滤出type = 'creditor'

selectaccount_id,type,amount,substr(day,1,4) year,substr(day,6,2) monthfrom Transactions
where type = 'creditor'
order by account_id,month;

第二步:将两张表进行关联,根据month,t1.account_id,max_income分组

with t1 as (selectaccount_id,type,amount,substr(day,1,4) year,substr(day,6,2) monthfrom Transactionswhere type = 'creditor'order by account_id,month
),t2 as (selectt1.account_id,sum(amount) max,month,max_incomefrom t1,Accountswhere t1.account_id = Accounts.account_idgroup by month,t1.account_id,max_incomeorder by t1.account_id)
select * from t2;

第三步:利用开窗函数row_number()形成一列排名

with t1 as (selectaccount_id,type,amount,substr(day,1,4) year,substr(day,6,2) monthfrom Transactionswhere type = 'creditor'order by account_id,month
),t2 as (selectt1.account_id,sum(amount) max,month,max_incomefrom t1,Accountswhere t1.account_id = Accounts.account_idgroup by month,t1.account_id,max_incomeorder by t1.account_id
),t3 as (select t2.account_id,t2.max,month,row_number() over(partition by t2.account_id order by t2.month) rn1from t2where max > max_income)select * from t3;

第四步:再得出一个等差数列month2,若是差值相同,则连续


with t1 as (selectaccount_id,type,amount,substr(day,1,4) year,substr(day,6,2) monthfrom Transactionswhere type = 'creditor'order by account_id,month
),t2 as (selectt1.account_id,sum(amount) max,month,max_incomefrom t1,Accountswhere t1.account_id = Accounts.account_idgroup by month,t1.account_id,max_incomeorder by t1.account_id
),t3 as (select t2.account_id,t2.max,month,row_number() over(partition by t2.account_id order by t2.month) rn1from t2where max > max_income
),t4 as (select account_id,(month - rn1) month2,count(month-rn1) as r2from t3group by account_id,(month - rn1)
)select * from t4;

实现

with t1 as (selectaccount_id,type,amount,substr(day,1,4) year,substr(day,6,2) monthfrom Transactionswhere type = 'creditor'order by account_id,month
),t2 as (selectt1.account_id,sum(amount) max,month,max_incomefrom t1,Accountswhere t1.account_id = Accounts.account_idgroup by month,t1.account_id,max_incomeorder by t1.account_id
),t3 as (select t2.account_id,t2.max,month,row_number() over(partition by t2.account_id order by t2.month) rn1from t2where max > max_income
),t4 as (select account_id,(month - rn1) month2,count(month-rn1) as r2from t3group by account_id,(month - rn1)
)selectdistinct account_idfrom t4
where r2 >=2;

总结

遇见连续性问题,需要两列差值相同的,最后进行相减,相同的即为连续。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1541263.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【Unity-UGUI组件拓展】| Image 组件拓展,支持FIlled和Slice功能并存

🎬【Unity-UGUI组件拓展】| Image 组件拓展,支持FIlled和Slice功能并存一、组件介绍二、组件拓展方法三、完整代码💯总结🎬 博客主页:https://xiaoy.blog.csdn.net 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN🙉 🎄 学习专栏推荐:Unity系统学习专栏 🌲 游戏…

C / C++的内存管理

前言 Hello,我又回来了,今天我们将继续学习C部分,今天我们将承接前面的知识,继续学习C的内存管理,今天的内容较为重要,所以我们废话不多说,我们还是按例三连上车,开始我们今天内容&…

Python中lambda表达式的使用——完整通透版

文章目录 一、前言二、 基本语法三、举个简单的例子:四、常见应用场景1. 用于排序函数sort() 方法简介lambda 表达式的作用详细解释进一步扩展总结 2、与 map、filter、reduce 等函数结合1、 map() 函数示例:将列表中的每个数字平方 2、 filter() 函数示…

Typora安装和导入导出

Typora安装和导入导出 文章目录 Typora安装和导入导出前言Typora v1.9.5Typora v1.4.7Pandoc 前言 Typora v1.9是最新版, , Typora v1.4是老版本的, 这两个选择一个即可Pandoc可以导入导出word Typora v1.9.5 Typora v1.9.rar, 提取码:tian按ctrl单击鼠标左键打开…

数据飞轮崛起:数据中台真的过时了吗?

一、数据中台的兴起与困境 随着大数据技术的不断发展,我见证了企业数据能力建设的演变。从数据中台的兴起,到如今数据飞轮模式的热议,企业的数据管理理念经历了巨大的变化。起初,数据中台作为解决数据孤岛、打破部门壁垒的“救星…

新版torch_geometric不存在uniform、maybe_num_nodes函数问题(Prune4ED论文报错解决)

这是在复现论文”“时遇到的报错。 ImportError: cannot import name uniform from torch_geometric.nn.pool.topk_pool 一、报错原因 论文作者使用的是2.1.0版本的torch_geometric。而我安装了2.6.1的torch_geometric。新版中已经去除了uniform和maybe_num_nodes这两个函数&…

力扣 中等 162.寻找峰值

文章目录 题目介绍解法 题目介绍 解法 定理&#xff1a;如果 i<n−1 且 nums[i]<nums[i1]&#xff0c;那么在下标 [i1,n−1] 中一定存在至少一个峰值。证明 思路分析&#xff1a;利用采用红蓝染色题体法&#xff0c;n为数组的长度&#xff0c;开始左指针L指向数组最左边…

CompletableFuture-详解使用及源码解析

背景 上一篇文章我们看了FutureTask&#xff0c;分析了他的问题&#xff0c;异步编程并不方便。 问题1&#xff1a; FutureTask获取执行结果前&#xff0c;主线程需要通过get()方法一直阻塞等待子线程执行完成call方法&#xff0c;才可以拿到返回结果问题2&#xff1a;如果不…

linux固定串口别名

最近项目功能要求&#xff0c;需要将插入设备的串口设备占用的端口号固定住&#xff0c;这里记录一下设置过程方便以后查阅。 linux固定串口别名 配置过程相关补充 配置过程 列出当前插入USB端口的设备&#xff1a; lsusb查看当前设备的端口号&#xff1a; ls dev/查看当前设…

【24华为杯数模研赛赛题思路已出】国赛D题思路丨附参考代码丨免费分享

2024年华为杯研赛C题解题思路 D 题 大数据驱动的地理综合问题 地理系统是自然、人文多要素综合作用的复杂巨系统[1-2]&#xff0c;地理学家常用地理综合的方式对地理系统进行主导特征的表达[3]。如以三大阶梯概括中国的地形特征&#xff0c;以秦岭—淮河一线和其它地理区划的…

一周热门|李飞飞:过于武断的AI政策将损害学术界和开源社区;纽约大学教授:我们可能都被奥特曼耍了

大模型周报将从【企业动态】【技术前瞻】【政策法规】【专家观点】四部分&#xff0c;带你快速跟进大模型行业热门动态。 01 企业动态 CogVideoX 2B&#xff1a;首个开源商用级视频生成模型 作为首个开源商用级视频生成模型&#xff0c;CogVideoX 2B 与智谱AI「清影」同源&a…

java项目编译UTF-8编译问题

代码没有报错信息,但是一启动就各种符号什么的报错,看代码也没有问题 然后就搜各种解决方案 我试了好几种,这种是生效的,直接在IDEA修改 没修改之前的配置 修改后的

一对一视频通话软件Call-Me

什么是 Call-Me &#xff1f; Call-Me 使你能够直接通过网页浏览器使用 WebRTC 技术轻松进行一对一的视频通话。 Call-Me 的主要功能&#xff1a; 使用用户名登录。通过输入接收者的用户名进行视频通话。切换视频源的可见性。通话结束时挂断电话。提供 REST API 获取所有连接…

【d46】【Java】【力扣】234.回文链表

思路 判断是否是回文&#xff0c;需要&#xff1a;一个指针指向头&#xff0c;一个指针指向尾&#xff0c;两个指针一边向中间靠拢&#xff0c;一边判断数值是否相同 对于单链表&#xff0c;不方便获得pre&#xff0c;如果将节点放进 数组/list &#xff0c;数组/list可以直接…

[Unity Demo]从零开始制作空洞骑士Hollow Knight第七集:制作小骑士完整的冲刺Dash行为

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、制作一个完整的小骑士冲刺Dash行为 1.制作动画以及使用UNITY编辑器编辑2.使用代码实现完整的冲刺行为控制总结 前言 大家又好久不见&#xff08;虽然就过了…

论文速递!基于PINN的知识+数据融合方法!实现可再生能源电力系统中的TTC高效评估

本期推文将介绍一种结合知识驱动和数据驱动的混合算法在电力系统总传输能力&#xff08;TTC&#xff09;评估中的应用&#xff0c;这项研究发表于《IEEE Transactions on Power Systems》期刊&#xff0c;主要解决高比例可再生能源渗透下电力系统中的TTC快速评估问题。 荐读的论…

day21JS-npm中的部分插件使用方法

1. 静态资源目录 静态资源目录就是访问服务器的某些路劲时候&#xff0c;服务器可以吐出一个写好的指定页面。 实现思路&#xff1a; 1、先判断要找的路径是否是文件&#xff0c;如果是文件&#xff0c;就加载发给对方。 2、如果是文件夹&#xff0c;找到这个文件夹所在路径中…

linux----进程地址空间

前言 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、空间分布 二、栈和堆的特点 &#xff08;1&#xff09;栈堆相对而生&#xff0c;堆是向上增长的&#xff0c;栈是向下增长的。 验证&#xff1a;堆是向上增长的 这里我们看到申请的堆&#xff…

springMvc的初始配置

基础文件结构(toWeb插件) 1.导入对应依赖 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"ht…

【软考】循环冗余校验码

目录 1. 说明2. CRC 的代码格式3. 例题3.1 例题1 1. 说明 1.循环冗余校验码(Cyclic Redundancy Check&#xff0c;CRC)广泛应用于数据通信领域和磁介质存储系统中。2.它利用生成多项式为k个数据位产生&#xff0c;r个校验位来进行编码&#xff0c;其编码长度为 kr。3.循环几余…