MySQL高阶1843-可疑银行账户

news2024/9/20 16:30:48

目录

题目

准备数据

​分析数据

实现

总结


题目

如果一个账户在 连续两个及以上 月份的 总收入 超过最大收入(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'

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

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

with t1 as (
    select
        account_id,type,amount,
        substr(day,1,4) year,
        substr(day,6,2) month
    from Transactions
    where type = 'creditor'
    order by account_id,month
),t2 as (
    select
        t1.account_id,sum(amount) max,month,max_income
    from t1,Accounts
    where t1.account_id = Accounts.account_id
    group by month,t1.account_id,max_income
    order by t1.account_id)
select * from t2;

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

with t1 as (
    select
        account_id,type,amount,
        substr(day,1,4) year,
        substr(day,6,2) month
    from Transactions
    where type = 'creditor'
    order by account_id,month
),t2 as (
    select
        t1.account_id,sum(amount) max,month,max_income
    from t1,Accounts
    where t1.account_id = Accounts.account_id
    group by month,t1.account_id,max_income
    order 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) rn1
    from t2
    where max > max_income)select * from t3;

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


with t1 as (
    select
        account_id,type,amount,
        substr(day,1,4) year,
        substr(day,6,2) month
    from Transactions
    where type = 'creditor'
    order by account_id,month
),t2 as (
    select
        t1.account_id,sum(amount) max,month,max_income
    from t1,Accounts
    where t1.account_id = Accounts.account_id
    group by month,t1.account_id,max_income
    order 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) rn1
    from t2
    where max > max_income
),t4 as (
    select account_id,(month - rn1) month2,
           count(month-rn1) as r2
    from t3
    group by account_id,(month - rn1)
)select * from t4;

实现

with t1 as (
    select
        account_id,type,amount,
        substr(day,1,4) year,
        substr(day,6,2) month
    from Transactions
    where type = 'creditor'
    order by account_id,month
),t2 as (
    select
        t1.account_id,sum(amount) max,month,max_income
    from t1,Accounts
    where t1.account_id = Accounts.account_id
    group by month,t1.account_id,max_income
    order 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) rn1
    from t2
    where max > max_income
),t4 as (
    select account_id,(month - rn1) month2,
           count(month-rn1) as r2
           from t3
    group by account_id,(month - rn1)
)select
     distinct account_id
     from t4
where r2 >=2;

总结

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

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

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

相关文章

Vivado FIR IP 详解 (一)

FIR滤波器是数字信号处理中常用的滤波器,除了通过Verilog代码自己实现外,Vivado提供了一个FIR滤波器 IP,可以直接调用。 一、什么是 Vivado FIR IP 核 FIR(Finite Impulse Response)滤波器即有限长单位冲激响应滤波器…

兔子检测系统源码分享

兔子检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vision …

安卓13设置动态显示隐藏第一页的某一项 动态显示隐藏无障碍 android13设置动态显示隐藏第一页的某一项

总纲 android13 rom 开发总纲说明 文章目录 1.前言2.问题分析3.代码分析4.代码修改4.1修改方法14.2修改方法25.编译6.彩蛋1.前言 有时候,我们的设置里面显示的信息,需要根据不同的情况显示不同的信息,例如,动态的显示或者隐藏 “无障碍” 这一项。 2.问题分析 像这个问题…

【经验帖】脏读和不可重复读的概念及影响

脏读和不可重复读是数据库事务并发执行时可能出现的两种数据一致性问题,它们对数据的一致性和完整性有着显著的影响。以下是脏读和不可重复读的具体影响: 脏读的影响 脏读发生在一个事务读取了另一个事务未提交的数据时。由于这些数据尚未被提交&#x…

python机器人编程——用手机web远程视频监控并控制小车驾驶(上篇vrep仿真)

目录 一、前言二、技术架构三、设备端实现四、服务控制端实现(1)摄像头服务模块(2)web服务器 五、web端实现(1)视频显示(2)驾驶盘的实现(3)心跳 六、总结七、…

大厂里为什么都在运用精益六西格玛管理?

近年来,大型企业(简称“大厂”)为了保持其市场领先地位和持续盈利能力,不断探索并引入先进的管理理念和方法。其中,精益六西格玛管理作为一种综合性的质量管理和流程优化工具,正被越来越多的企业所采用。本…

YOLOv8改进系列,YOLOv8 Neck结构引入BiFPN

摘要 模型效率在计算机视觉中变得越来越重要。本文系统地研究了神经网络架构设计选择用于目标检测,并提出了几项关键优化以提高效率。首先,提出了一种加权双向特征金字塔网络(BiFPN),它允许轻松快速的多尺度特征融合;其次,我们提出了一种复合缩放方法,该方法同时均匀地…

操作系统 --- 进程的同步和互斥问题以及进程互斥实现方法(软件、硬件实现)、同步机制遵循的四条准则

目录 一、进程同步 二、进程互斥 三、进程互斥的实现方法 3.1 软件实现 3.1.1 单标志法(存在的主要问题:违背“空闲让进”原则) 3.1.1.1 基本思想 3.1.1.2 单标志法的基本概念及执行流程 3.1.1.3 特点 3.1.2 双标志先检查法&#…

【Linux 】开发利器:深度探索 Vim 编辑器的无限可能

🔥 个人主页:大耳朵土土垚 🔥 所属专栏:Linux系统编程 这里将会不定期更新有关Linux的内容,欢迎大家点赞,收藏,评论🥳🥳🎉🎉🎉 文章目…

NC65使用UAP客开主子单据以及NC65常见的开发技术(超级详细附带图和源码)

NC65使用UAP客开主子单据以及NC65单据相关的开发技术(超级详细附带图和源码) 本篇主要讲述使用UAP开发工具客开主子单据包括:创建项目发布、创建发布元数据、生成主子单据、以及相关报错和打补丁。在单据相关技术主要包括:增加空按…

物流管理系统小程序的设计

管理员账户功能包括:系统首页,个人中心,用户管理,员工管理,部门管理,物品分类管理,物流公司管理,物流信息管理,配送信息管理 微信端账号功能包括:系统首页&a…

半导体器件制造5G智能工厂数字孪生物联平台,推进制造业数字化转型

半导体器件制造行业作为高科技领域的核心驱动力,正积极探索和实践以5G智能工厂数字孪生平台为核心的新型制造模式。这一创新不仅极大地提升了生产效率与质量,更为制造业的未来发展绘制了一幅智能化、网络化的宏伟蓝图。 在半导体器件制造5G智能工厂中&a…

python 山峦图

效果: 代码: import matplotlib.pyplot as plt import numpy as npdef mountain_plot(data_dict, colorsNone):if colors is None:colors get_colors_from_map(len(data_dict), "Spectral")x list(data_dict.keys())# Y轴位置y_positions …

某锂电厂房项目密集母线槽上红外测温的案例分享

1 行业背景 在政策和技术推动下,锂电产业迅速发展,产业规模持续扩大,同时对供电设备的可靠性要求提高。密集型母线槽作为厂房重要电力传输设备若出现触头温升过高,可能导致停电甚至烧毁等故障,会对生产线安全和企业效…

Java反序列化漏洞分析

相关学习资料# http://www.freebuf.com/vuls/90840.htmlhttps://security.tencent.com/index.php/blog/msg/97http://www.tuicool.com/articles/ZvMbInehttp://www.freebuf.com/vuls/86566.htmlhttp://sec.chinabyte.com/435/13618435.shtmlhttp://www.myhack58.com/Article/ht…

【Qt笔记】QTabWidget控件详解

目录 引言 一、基本功能 二、核心属性 2.1 标签页管理 2.2 标签位置 2.3 标签形状 2.4 标签可关闭性 2.5 标签可移动性 三、信号与槽 四、高级功能 4.1 动态添加和删除标签页 4.2 自定义标签页的关闭按钮行为 4.3 标签页的上下文菜单 五、样式设置 六、应用示例…

git使用“保姆级”教程1——简介及配置项设置

一、git介绍 Git是一个开源的分布式版本控制系统,用于:敏捷高效地处理任何或小或大的项目。Git 是Linus Torvalds 为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。版本控制: 版本控制(Revision control&#xff…

鸿蒙环境服务端签名直传文件到OSS

本文介绍如何在鸿蒙环境下将文件上传到OSS。 背景信息 鸿蒙环境是当下比较流行的操作环境,与服务端签名直传的原理类似,鸿蒙环境上传文件到OSS是利用OSS提供的PutObject接口来实现文件上传到OSS。关于PutObject的详细介绍,请参见PutObject。…

大厂常问的MySQL事务隔离到底怎么回答

什么是事务 事务就是一组原子性的SQL查询,或者说一个独立的工作单元。事务内的语句,要么全部执行成功,要么全部执行失败。 关于事务银行系统的应用是解释事务必要性的一个经典例子。 假设一个银行的数据库有两张表:支票表&#x…

OpenAI o1大模型:提示词工程已死

OpenAI 最近发布了最新大模型 o1,通过强化学习训练来执行复杂的推理任务,o1 在多项基准测试中展现了博士级别的推理能力,甚至在某些情况下可以与人类专家相媲美。 当你使用 o1 的时候,会发现文档中多了一项提示词建议。 翻译一下&…