leetcode 1843 可疑银行账户(postgresql)

需求

表: 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 是转账的存取金额。

写一个SQL查询语句列示所有的可疑账户。

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

返回的结果表以transaction_id 排序。

查询结果格式如下。

示例 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不列入结果表中。

输入

输出

sql 复制代码
with t1 as (
-- 筛选出需要的数据
select account_id,amount,to_char(day,'YYYY') as day_year,to_char(day,'mm') as day_month
from transactions
where type='creditor'
),t2 as (
-- 计算每个账户每月的收入数据
select account_id,day_year,day_month,sum(amount) as amount
from t1
group by account_id,day_year,day_month
),t3 as (
-- 添加一列,根据账户分组,年月排序,并求出大于最大值的数据
select t2.*,a.max_income,
       row_number() over (partition by a.account_id order by day_month,day_month) as rn1
from t2,accounts a
where t2.account_id=a.account_id and t2.amount>max_income
),t4 as (
-- 将月份减去新添加的一列,得到的数据相同的,为相邻的月份
select *,day_month::bigint-rn1 as rn2
from t3
)
-- 聚合求出所要的id
select account_id
from t4
group by account_id,day_year,rn2
having count(1)>1
order by account_id
相关推荐
C137的本贾尼4 分钟前
(每日一道算法题)二叉树剪枝
算法·机器学习·剪枝
文牧之1 小时前
Oracle 审计参数:AUDIT_TRAIL 和 AUDIT_SYS_OPERATIONS
运维·数据库·oracle
篱笆院的狗1 小时前
如何使用 Redis 快速实现布隆过滤器?
数据库·redis·缓存
BUG收容所所长2 小时前
栈的奇妙世界:从冰棒到算法的华丽转身
前端·javascript·算法
XRZaaa2 小时前
常见排序算法详解与C语言实现
c语言·算法·排序算法
@我漫长的孤独流浪2 小时前
数据结构测试模拟题(4)
数据结构·c++·算法
智驱力人工智能2 小时前
智慧零售管理中的客流统计与属性分析
人工智能·算法·边缘计算·零售·智慧零售·聚众识别·人员计数
洛神灬殇2 小时前
【LLM大模型技术专题】「入门到精通系列教程」基于ai-openai-spring-boot-starter集成开发实战指南
网络·数据库·微服务·云原生·架构
小鸡脚来咯2 小时前
redis分片集群架构
数据库·redis·架构
christine-rr3 小时前
征文投稿:如何写一份实用的技术文档?——以软件配置为例
运维·前端·网络·数据库·软件构建