外贸公司网站如何免费推广,域名注册在那个网站好,贵州城乡建设官方网站,家在深圳光明论坛力扣题
1、题目地址
1843. 可疑银行账户
2、模拟表
表#xff1a;Accounts
Column NameTypeaccount_idintmax_incomeint
account_id 是这张表具有唯一值的列。每行包含一个银行账户每月最大收入的信息。
表#xff1a;Transactions
Column NameTypetransaction_idint…力扣题
1、题目地址
1843. 可疑银行账户
2、模拟表
表Accounts
Column NameTypeaccount_idintmax_incomeint
account_id 是这张表具有唯一值的列。每行包含一个银行账户每月最大收入的信息。
表Transactions
Column NameTypetransaction_idintaccount_idinttypeENUMamountintdaydatetime
transaction_id 是这张表具有唯一值的列。每行包含一条转账信息。type 是枚举类型包含’Creditor’,‘Debtor’其中 ‘Creditor’ 表示用户向其账户存入资金‘Debtor’ 表示用户从其账户取出资金。amount 是交易过程中的存入/取出的金额。
3、要求
如果一个账户在 连续两个及以上 月份的 总收入 超过最大收入max_income那么认为这个账户 可疑。 账户当月 总收入 是当月存入资金总数即 transactions 表中 type 字段的 ‘Creditor’。
编写一个解决方案报告所有的 可疑 账户。
以 任意顺序 返回结果表
返回结果格式如下示例所示。
示例 1
输入 Accounts 表
account_idmax_income321000410400
Transactions 表
transaction_idaccount_idtypeamountday23Creditor1071002021-06-02 11:38:1444Creditor104002021-06-20 12:39:18114Debtor588002021-07-23 12:41:5514Creditor493002021-05-03 16:11:04153Debtor755002021-05-23 14:40:20103Creditor1021002021-06-15 10:37:16144Creditor563002021-07-21 12:12:25194Debtor1011002021-05-09 15:21:4983Creditor649002021-07-26 15:09:5673Creditor909002021-06-14 11:23:07
输出
account_id3
解释
对于账户 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不列入结果表中。
4、代码编写
思路
1、Transactions 中根据 account_id 和 day里面的年月进行分组加上查询条件 type‘Creditor’存入的钱就可以查询到不同账户在不同月份存钱的总量
SELECT account_id, DATE_FORMAT(day, %Y-%m) AS month, SUM(amount) AS sAmount
FROM Transactions
WHERE type Creditor
GROUP BY account_id, month| account_id | month | sAmount |
| ---------- | ------- | ------- |
| 3 | 2021-06 | 300100 |
| 4 | 2021-06 | 10400 |
| 4 | 2021-05 | 49300 |
| 4 | 2021-07 | 56300 |
| 3 | 2021-07 | 64900 |2、要求里面是要连续两个月或以上总收入超过最大收入那我们按最少两个月就行将上面查询出来的连接自己account_id 相同月份month差距是一个月满足总收入sAmount超过最大收入max_income就行
第一版写法日期 YYYY-MM 格式特殊处理
WITH tmp AS(SELECT account_id, DATE_FORMAT(day, %Y-%m) AS month, SUM(amount) AS sAmountFROM TransactionsWHERE type CreditorGROUP BY account_id, month
)
SELECT DISTINCT one.account_id
FROM tmp AS one, tmp AS two
WHERE one.account_id two.account_id
AND CONCAT(one.month, 01) DATE_SUB(CONCAT(two.month, -01), INTERVAL 1 MONTH)
AND one.sAmount (SELECT max_income FROM Accounts WHERE account_id one.account_id)
AND two.sAmount (SELECT max_income FROM Accounts WHERE account_id two.account_id)说一下我为什么这样用 首先我测试 select date_sub(2023-02, interval 1 month)期望结果是 2023-01但是结果是 Null 其次我再测试 select date_sub(2023-02-01, interval 1 month)这次正常输出结果是 2023-01-01 所以就有了个想法拼接后面的 -01使用 concat 函数进行拼接即可即 select date_sub(concat(2023-02,-01), interval 1 month)
第二版写法日期格式用 YYYYMM 使用 PERIOD_DIFF 函数
PERIOD_DIFF 1、PERIOD_DIFF() 函数返回两日期之间的差异结果以月份计算。 2、period1 和 period2 应采用相同的格式格式为YYMM或YYYYMM 3、语法PERIOD_DIFF(period1, period2)
测试 select period_diff(202302, 202301)结果 1 select period_diff(2302, 2301)结果 1 select period_diff(202301, 202302)结果 -1 select period_diff(2301, 2302)结果 -1
参考MySQL PERIOD_DIFF() 函数
WITH tmp AS(SELECT account_id, DATE_FORMAT(day, %Y%m) AS month, SUM(amount) AS sAmountFROM TransactionsWHERE type CreditorGROUP BY account_id, month
)
SELECT DISTINCT one.account_id
FROM tmp AS one, tmp AS two
WHERE one.account_id two.account_id
AND PERIOD_DIFF(two.month, one.month) 1
AND one.sAmount (SELECT max_income FROM Accounts WHERE account_id one.account_id)
AND two.sAmount (SELECT max_income FROM Accounts WHERE account_id two.account_id)注意日期转换 %Y 和 %m 对应 YYYY 和 MMmm是分钟MM才是月份注意不要弄错
date_format( ) 转换格式
格式描述%M月名%m月数值00-12%Y年4 位%y年2 位