LeetCode_sql_day17(1843.可疑银行账户)

news2024/9/20 20:28:42

描述:

表: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

图解:

代码:

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

;

总结:

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

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

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

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

相关文章

提供开发资料 Hi3516CV610-00B/10B/20B/00S/20S/00G/20G 七个型号配置差异

根据功能不同, Hi3516CV610 分为七个不同型号版本: HI3516CV610-00B HI3516CV610-00B HI3516CV610-10B HI3516CV610-20B HI3516CV610-00S HI3516CV610-20S HI3516CV610-00G HI3516CV610-20G

【书生2.1】书生大模型全链路开源体系

0 引言 书生浦语官网 开源一周年总结及回顾 1 回顾 1.1 社区生态 2 总结 书生浦语大模型的开源开放体系,包括技术发展、性能提升、模型架构、开源生态等。 要点: 🌟 开源开放体系涵盖数据收集、标注、训练、微调、评测、部署等全链路。 &#x1f68…

【案例64】无法从套接字读取更多的数据

问题现象 系统突然间登录报如下错误:SELECT * FROM sm_user WHERE user_code_q? 无法从套接字读取更多的数据 问题分析 查看nc-log.log发现大量相关报错 $$callid1723104097968-1063 $$thread[http-bio-xxx-xxx-exec-xxx] $$hostxxx$$userid#UAP# $$tsxxx-08-08…

C++竞赛初阶L1-14-第六单元-数组(31~33课)542: T456472 数组逆序重存放

题目内容 将一个数组中的值按逆序重新存放。例如&#xff0c;原来的顺序为 8,6,5,4,1。要求改为 1,4,5,6,8。 输入格式 输入为两行&#xff1a;第一行数组中元素的个数 n&#xff08;1<n≤100)&#xff0c;第二行是 n 个整数&#xff0c;每两个整数之间用空格分隔。 输出…

Windows安装PostgreSQL数据库,保姆级教程

PostgreSQL 是客户端/服务器关系数据库管理系统 (RDMS)。PostgreSQL是一个功能非常强大的、源代码开放的客户/服务器关系型数据库管理系统&#xff08;RDBMS&#xff09;。PostgreSQL 也有自己的查询语言&#xff0c;称为 pgsql。 此外&#xff0c;PostgreSQL 还支持过程语言&a…

Cesium模型封装-Point

一、初始化地图 <template><div class"cesium_map"><div id"cesiumContainer"></div></div> </template><script setup> import { reactive, ref, onMounted } from "vue"; import { Point } from &…

基于yolov8的安全帽反光衣护目镜检测系统python源码+onnx模型+评估指标曲线+精美GUI界面

【算法介绍】 基于YOLOv8的安全帽、反光衣及护目镜检测系统是一款集成了前沿深度学习与计算机视觉技术的智能监控系统。该系统利用YOLOv8这一尖端的目标检测模型&#xff0c;结合云计算与自动化图像处理技术&#xff0c;实现对工地、化工厂、煤矿等高风险作业区域工作人员安全…

Java—方法引用

目录 初识方法引用 方法引用的分类 引用静态方法 引用成员方法 引用构造方法 其它调用方式 类名引用成员方法 引用数组的构造方法 总结 初识方法引用 方法引用就是拿现有的方法来当做函数式接口中抽象方法的方法体。 方法引用注意事项 1. 引用处必须是函数式接口&a…

初识JAVA(上)

&#x1f381;&#x1f381;创作不易&#xff0c;关注作者不迷路&#x1f380;&#x1f380; 初识JAVA 前言一、初识JAVA1.1.Java是什么1.2.Java语言的重要性1.3 Java语言发展简史1.4 Java语言特性 二、初识Java的main方法1 main方法示例 三、注释基本规则 四、数据类型1.常量2…

入门Java第一步—>IDEA的下载与安装与JDK的环境配置(day01)

1.JDK的下载与安装 jdk的安装链接分为不同操作系统如下,点击链接跳转下载页面&#xff1a; windows操作系统JDK下载链接(按住键盘ctrl键单击链接即可)&#xff1a; 链接7天有效&#xff0c;有需要的评论区找我哈 通过网盘分享的文件&#xff1a;jdk-8u271-windows-x64.exe 链…

建筑企业数字信息化转型的建议

在现代建筑企业的管理中&#xff0c;信息化转型已成为提升效率和竞争力的关键。然而&#xff0c;在选择信息化系统时&#xff0c;企业需要慎重考虑&#xff0c;以确保系统真正适合企业的现状和未来发展。 &#x1f50d; 要选合适的&#xff0c;而非“成熟”的 信息化系统的核心…

解决 启动模拟器出现 未开启Hyper-V 的问题

~~ 解决 启动模拟器出现 未开启Hyper-V 的问题 ~~ 如果在启动模拟器时出现 未开启Hyper-V 的问题 解决方案&#xff1a; 1.打开控制面板–>点击 程序和功能 2.点击左侧&#xff1a;启用或关闭Windows功能 3.找到虚拟机平台–> 打对勾√ -->确定 &#xff08;注意…

harbor私有仓库管理(twenty-nine day)

一、harbor私有仓库管理 是python的包管理工具&#xff0c;和yum对redhat的关系是一样的 yum -y install epel-release yum -y install python2-pip pip install --upgrade pip pip list pip 8x pip install --upgrade pip pip install --upgrade pip20.3 -i https://mirror…

ElasticSearch学习笔记(四)分页、高亮、RestClient查询文档

文章目录 前言7 搜索结果处理7.2 分页7.2.1 基本使用7.2.2 深度分页7.2.3 小结 7.3 高亮7.3.1 高亮原理7.3.2 实现高亮 8 RestClient查询文档8.1 match_all查询8.2 match查询与multi_match查询8.3 精确查询8.4 布尔查询8.5 排序、分页、高亮 9 项目实战9.1 酒店搜索和分页9.2 酒…

Linux 软件包管理器yum 自动化构建工具-make/makefile

Linux 工具 linux 软件包管理器 yum 把一些常用的软件提前编译好&#xff0c;做成软件包放在一个服务器上&#xff0c;通过包管理器可以很方便的获取到在这个编译好的软件包。直接进行安装。 软件包和软件包管理器就相当于 App 和应用商店这样的关系。 Linux 安装软件 源代码…

【QT】学习笔记:导出资源中静态文件

在 Qt C 中&#xff0c;可以通过将文件添加到资源文件中&#xff0c;并在程序运行时将其导出到磁盘上的指定目录。以下是具体的步骤和代码示例&#xff1a; 1. 将文件添加到资源文件中 首先&#xff0c;需要将文件添加到 Qt 的资源系统中。假设你已经创建了一个资源文件&…

力扣经典题目之->对称二叉树(镜像二叉树)

一&#xff1a;题目 本题只需在此题上稍作修改即可&#xff1a;力扣经典题目之-&#xff1e;相同的树&#xff08;递归判断两颗二叉树是否相同&#xff09;-CSDN博客 二&#xff1a;代码 解释&#xff1a; 1&#xff1a;对称二叉树本质就是左右子树的对比&#xff0c;但不是…

Golang使用Quic-Go开源库实现Quic客户端和服务端

Quic-Go介绍 Quic-Go是Go语言Quic协议&#xff08;RFC 9000、RFC 9001、RFC 9002&#xff09;的实现。它支持HTTP/3&#xff08;RFC 9114&#xff09;&#xff0c;包括QPACK&#xff08;RFC 9204&#xff09;和HTTP数据报&#xff08;RFC 9297&#xff09;。 Github地址 htt…

谷歌发布 3 款 Gemini 新模型;字节开源 FLUX Dev Hyper SD Lora,8 步生图丨 RTE 开发者日报

开发者朋友们大家好&#xff1a; 这里是 「RTE 开发者日报」 &#xff0c;每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享 RTE&#xff08;Real-Time Engagement&#xff09; 领域内「有话题的 新闻 」、「有态度的 观点 」、「有意思的 数据 」、「有思考的 文…

源代码防泄露迎来信创时代:信创沙箱

在当今数字化时代&#xff0c;信息安全已成为企业生存与发展的基石&#xff0c;尤其是在信息技术应用创新&#xff08;信创&#xff09;环境下&#xff0c;数据保护更是被提升至前所未有的高度。SDC沙盒防泄密系统以其独特的技术架构和卓越的安全性能&#xff0c;在信创环境中构…