Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

51 可疑银行账号 #63

Open
astak16 opened this issue Mar 7, 2022 · 0 comments
Open

51 可疑银行账号 #63

astak16 opened this issue Mar 7, 2022 · 0 comments
Labels

Comments

@astak16
Copy link
Owner

astak16 commented Mar 7, 2022

题目

题目链接:可疑银行账户

type 是枚举类型(包含 'Creditor''Debtor'),其中 'Creditor' 表示用户向其账户存入资金,'Debtor' 表示用户从其账户取出资金。

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

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

返回的结果表以 transaction_id 排序。

create table accounts (
  account_id int,
  max_income int
)
insert into accounts (account_id, max_income) values
(3, 21000),
(4, 10400);

create table transactions (
  transaction_id int,
  account_id int,
  type ENUM('creditor', 'debtor'),
  amount int, day datetime
)
insert into transactions (transaction_id, account_id, type, amount, day) values
(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');

分析

这题考的是如何判断月份是否连续,只需要两个月连续,这个用户就需要查询出来

判断下一个月的方法核心是 period_add ,所以需要有两个字段 monthnext_monthmonth 字段可以通过内部函数进行转换就可以拿到了,复杂的地方就在于 next_month 怎么拿到:

  • 方法一获取下个月是使用子查询
  • 方法二获取下个月是使用自连接
  • 方法四获取下个月时使用窗口函数

方法三和其他三个方法不一样,它没有获取 next_month ,而是对每个用户的所有月份进行了排序,从而得到连续的月份。

SQL:方法一

with temp as (
  select
    account_id,
    date_format(day, '%Y%m') yearmonth
  from transactions join accounts using(account_id)
  where type = 'creditor'
  group by account_id, yearmonth
  having sum(amount) > max(max_income)
)
select
  distinct account_id
from temp where (account_id, period_add(yearmonth, 1)) in (
  select account_id, yearmonth from temp
);

解析

使用子查询的方式查询出结果

  1. 使用 with 创建临时表 temp
    • 输出字段为 account_idyearmonth
    • 使用 group by 分支,分组的条件是 account_idyearmonth
    • 使用 having 筛选出每个用户每个月的总收入是否大于这个用户这个的最大收入
      • 使用 sum 是因为每个用户每个月可能有多笔收入,需要将它们每一笔都相加
      • max 可以替换为 min 或者 any_value 因为每个用户每个月的最大收入是一样的,取出其中一个就可以了
  2. 使用子查询查出 account_idyearmonth
    • where 如果要筛选多个条件,需要用括号括起来并且使用 in 操作符
    • period_add(yearmonth, 1) 的结果是下一个月
  3. 使用 distinct 去重,因为一个用户可能有多个连续月收入可疑,但是他们又不连续,查询出的结果,一个用户就会出现多次。

SQL:方法二

with temp as (
  select
    account_id,
    date_format(day, '%Y%m') yearmonth
  from transactions join accounts using(account_id)
  where type = 'creditor'
  group by account_id, yearmonth
  having sum(amount) > max(max_income)
)
select
  distinct account_id
from temp t1 join temp t2 using(account_id)
where t1.yearmonth = t2.yearmonth + 1;

-- where 可改成
period_diff(t1.yearmonth, t2.yearmonth) = 1

解析

使用两表连接的方式查询出结果

  1. 临时表 temp 和方法一是一样的
  2. 将临时表自连接,筛选条件是 t1.yearmonth = t2.yearmonth + 1
    1. t2.yearmonth + 1t1.yearmonth 下一个月
    2. 这中写法是有问题的,如果两个月份正好是年末和年尾,靠 +1 判断是否是下个月就判断不出来了
    3. 替代方法是 period_diff(t1.yearmonth, t2.yearmonth) = 1
  3. 使用 distinct 去重,因为一个用户可能有多个连续月收入可疑,但是他们又不连续,查询出的结果,一个用户就会出现多次。

SQL:方法三

select
  distinct account_id
from (
  select
    account_id,
    month,
    month - cast(row_number() over(partition by account_id) as signed) diff
  from (
    select
      account_id,
      month(day) month
    from accounts join transactions using(account_id)
    where type = 'creditor'
    group by account_id, month
    having sum(amount) > max(max_income)
    order by account_id, month
  ) temp1
) temp2
group by account_id, diff
having count(diff) >= 2;

解析

判断连续的数方法:将数进行升序或者降序排列,然后用这个数减去排列的序号,如果是连续的数,那么它们的结果是相同的。

num  |  rank  |  diff
1    |  1     |  0
2    |  2     |  0
3    |  3     |  0
4    |  4     |  0
6    |  5     |  1
7    |  6     |  1
8    |  7     |  1
10   |  8     |  2 
11   |  9     |  2

从上面可以得出 1 - 46 - 810 - 11 是连续的数

  1. 临时表 temp1 需要提供 monthaccount_id 这两个字段
    • accountstransactions 两表连接,筛选出 type = 'creditor' 的数据
    • 使用 month() 计算出月份
    • 按照 account_idmonth 进行分组
    • 筛选出收入异常的数据
  2. 临时表 temp2 需要提供 account_idmonthdiff 这三个字段
    • diff 字段使用 row_number() 按照 account_id 分组,就能得到这个用户月份收入的排名
      • 这里需要使用 cast(... as signed) 转换下类型(为啥要转换类型,目前不清楚)
  3. 查询临时表 temp2
    • 使用 distinct 的作用是,一名用户可能存在多个连续 2 个月以上的可疑收入,但是这里只需要一个 account_id ,所以需要去重
    • 按照 account_iddiff 进行分组,因为通过第 2 步可以得到连续的月份, diff 是相等的
    • 筛选出 diff 大于等于 2 的数据,就是可疑用户

SQL:方法四

select
  distinct account_id
from (
  select
    account_id,
    max_income,
    month,
    income,
    lead(month, 1, 0)
      over (partition by account_id order by account_id, month ②) as next_month
  from (
    select
      account_id,
      max_income,
      date_format( day, '%y%m' ) as 'month',
      sum(amount)
        over (partition by account_id, date_format(day, '%y%m')) as income
    from accounts join transactions using(account_id)
    where type = 'creditor'
    -- order by month ①
  ) as temp1 where income > max_income
) as temp2
where next_month != 0 and period_diff(next_month, month) = 1

解析

使用 lead 窗口函数实现的,通过窗口函数对相同的 account_id 进行排序后,将下一行的 month 作为当前行的 next_month

  • 使用 lag 也是可以实现的,下面步骤解释是使用 lead 做的
    lag(month, 1, 0)
      over (partition by account_id order by account_id, month desc) as next_month

但是这个方法折磨了我好久,主要是排序的问题 ② 处,用 lead() 不在 over 中使用 order by ,输出的顺序可能不是你想要的。

  1. temp1 表:查询出每个用户每个月的总收入 income,以及将日期进行格式化。

    • 聚合函数也是可以使用 over 的,省去了后面用 group by 分组的步骤(简化查询)
  2. temp2 表:使用窗口函数 lead() 按照用户分组,月份升序排列,会得到 next_month

    • 使用 lead(month, 1, 0) ,因为是按照升序排列,所以 next_month 就是当前行下面一行

      lead(month, 1, 0)
        over (partition by account_id order by account_id, month) as next_month
      
      -- 示例,不代表真实的查询结果
      month  |  next_month
      2105   |  2016
      2106   |  2107
      2107   |  0
    • 筛选出当月总收入 income 大于当月最大收入 max_income

      • 为什么 temp1 表中已经查询出 incomemax_income ,不能在 temp1 表中晒寻呢,而是要跑到 temp2 中去做呢?因为这涉及到 SQL 执行的顺序,在执行 where 时,窗口函数还没有运行,所以是拿不到 income 的,也就是说不能在 temp1 做这个筛选
  3. period_diff(next_month, month) = 1 筛选出连续两月都超过的总收入的记录

    • 为什么前面用 next_month != 0 是因为 period_diff 不能接受 0 作为参数,所以要把 next_month = 0 的去掉,同时也说明 next_month = 0 的记录是正常记录,不是可疑记录
    • 使用 distinct account_id ,因为题目只要求查找出 account_id,但是有可能一个用户可能有多个连续月存在可以,所以就需要使用 distinct 去重

心路历程:

毫无头绪

account_id = 12 的用户是可疑账户,每个月的 amount 都大于它的 max_income

account_id  |  amount  |  day         |  max_income
12          |  80200   |  2021-05-15  |  11000
12          |  22800   |  2021-06-17  |  11000
12          |  53000   |  2021-07-09  |  11000
12          |  18100   |  2021-07-11  |  11000
12          |  77700   |  2021-07-17  |  11000
12          |  55100   |  2021-07-31  |  11000

这里用 lead(month, 1, 0) over(partition by account_id) 筛选不出来,这里是倒序排列的

account_id  |  max_income  |  month  |  income  |  next_month
12          |  11000       |  2107   |  203900  |  2107
12          |  11000       |  2107   |  203900  |  2107
12          |  11000       |  2107   |  203900  |  2107
12          |  11000       |  2107   |  203900  |  2106
12          |  11000       |  2106   |  22800   |  2105
12          |  11000       |  2105   |  80200   |  0

我想到可以用 order bymonth 排序一下,是不是就可以了?

通过在 ① 处加了 order by monthaccount_id = 12 这条数据筛选出来了,我以为没问题了,提交代码运行,又有一条 account_id = 2 是一个可疑账户,我的代码没有查询出来。

这什么情况啊,一度怀疑不能用 lead 窗口函数。

这是 account_id = 2 的数据, 567 月,连续三个月超过最大收入:

account_id  |  amount  |  day         |  max_income
2           |  12100   |  2021-05-07  |  3000
2           |  77900   |  2021-05-13  |  3000
2           |  104200  |  2021-06-07  |  3000
2           |  3900    |  2021-07-01  |  3000
2           |  96000   |  2021-07-02  |  3000
2           |  30300   |  2021-07-22  |  3000

加上 ① 处 order by ,查询出来的顺序乱掉了 ,month 是无序的:

  • ps:我不知道使用了 lead 后这个顺序是怎么来的,我以为还会保持 temp1 表中的顺序
account_id  |  max_income  |  month  |  income  |  next_month
2           |  3000        |  2105   |  90000   |  2107
2           |  3000        |  2107   |  130200  |  2105
2           |  3000        |  2105   |  90000   |  2107
2           |  3000        |  2107   |  130200  |  2107
2           |  3000        |  2107   |  130200  |  2106
2           |  3000        |  2106   |  104200  |  0

然后我就准备放弃了,使用窗口函数问题太多了吧,正准备放弃的时候,我想到了在 over 中使用 order by 排序,顺序会怎样

峰回路转

我期望 month 顺序是正序排列。然后将下一行作为 next_month ,后面才能通过比较 next_monthmonth 确定两个月是否连续。

然后就在 ② 处添加了排序, account_id = 2 查询出来了,问题解决了。

提交运行,通过~~~

既然顺序不是我预期的,那就需要使用 order by 进行排序 ② 处

总结

lead 窗口函数了解不深,不太会使用,忘记了在 over 中是可以使用 order by

测试数据

SQL1

truncate table accounts;
insert into accounts (account_id, max_income) values
(59, 73000), (63, 28000), (46, 20000), (43, 31000), (54, 25000), (26, 29000), (51, 101000), (55, 46000), (68, 105000), (39, 72000), (19, 42000), (22, 49000), (44, 12000), (58, 86000), (70, 5000), (74, 22000), (48, 85000), (27, 96000), (61, 87000), (41, 97000), (67, 85000), (42, 59000), (15, 1000), (8, 35000), (38, 29000), (23, 7000), (66, 15000), (33, 61000), (32, 105000), (52, 40000), (28, 83000), (17, 28000), (50, 36000), (49, 30000), (35, 88000), (1, 34000), (3, 10000)

truncate table transactions;
insert into transactions (transaction_id, account_id, type, amount, day) values
(158, 52, "Debtor", 77900, "2021-02-23 15:30:43"), (356, 43, "Creditor", 61200, "2021-06-22 16:25:11"), (182, 61, "Debtor", 25700, "2021-10-27 12:08:03"), (228, 48, "Debtor", 64300, "2021-09-19 10:28:24"), (30, 15, "Creditor", 103400, "2021-04-16 10:10:47"), (252, 50, "Debtor", 92500, "2021-08-14 13:58:29"), (298, 8, "Creditor", 91500, "2021-06-19 10:29:01"), (348, 49, "Debtor", 107100, "2021-06-30 09:11:44"), (191, 41, "Creditor", 101000, "2021-06-13 15:37:02"), (57, 28, "Creditor", 26300, "2021-06-05 16:27:35"), (34, 52, "Debtor", 26000, "2021-07-07 11:23:53"), (93, 52, "Creditor", 9600, "2021-09-10 09:27:19"), (13, 58, "Debtor", 66500, "2021-10-15 17:52:49"), (235, 23, "Debtor", 85500, "2021-02-14 14:43:08"), (156, 59, "Debtor", 58700, "2021-07-05 15:50:26"), (216, 22, "Creditor", 82600, "2021-09-16 09:53:32"), (284, 41, "Debtor", 103500, "2021-05-13 08:43:13"), (42, 17, "Creditor", 82100, "2021-03-15 15:35:28"), (44, 61, "Debtor", 76200, "2021-03-22 09:12:43"), (303, 49, "Debtor", 26900, "2021-03-25 15:41:37"), (294, 41, "Creditor", 2000, "2021-12-05 09:56:51"), (112, 44, "Creditor", 60300, "2021-09-18 11:32:29"), (130, 1, "Debtor", 15100, "2021-07-10 11:12:02"), (219, 61, "Debtor", 96800, "2021-12-18 17:02:29"), (174, 67, "Creditor", 71300, "2021-10-06 17:06:13"), (288, 61, "Debtor", 74900, "2021-12-04 09:17:18"), (165, 50, "Debtor", 34900, "2021-09-20 08:47:11"), (310, 15, "Debtor", 22700, "2021-01-01 12:58:46"), (208, 68, "Creditor", 80000, "2021-12-23 16:56:54"), (334, 58, "Debtor", 42800, "2021-08-21 14:22:35"), (147, 67, "Creditor", 73800, "2021-02-08 08:57:45"), (107, 32, "Creditor", 62800, "2021-07-26 10:56:01"), (65, 67, "Creditor", 66000, "2021-04-30 11:56:56"), (293, 8, "Debtor", 56800, "2021-05-20 12:20:24"), (10, 28, "Creditor", 68600, "2021-05-19 16:06:12"), (164, 58, "Creditor", 49800, "2021-06-27 09:41:44"), (49, 63, "Debtor", 93700, "2021-11-17 16:31:54"), (114, 28, "Creditor", 40400, "2021-11-17 08:14:55"), (155, 66, "Debtor", 23900, "2021-12-02 17:50:51"), (345, 59, "Creditor", 41900, "2021-03-01 16:28:57"), (295, 46, "Debtor", 77000, "2021-05-27 13:26:43"), (315, 67, "Creditor", 92900, "2021-08-10 15:35:44"), (149, 54, "Debtor", 91700, "2021-07-03 13:49:26"), (181, 22, "Creditor", 25900, "2021-06-08 11:17:04"), (220, 15, "Creditor", 99100, "2021-03-29 09:30:14"), (256, 27, "Debtor", 59700, "2021-06-07 09:43:18"), (46, 63, "Creditor", 14100, "2021-07-28 15:21:39"), (221, 51, "Debtor", 35600, "2021-03-27 17:29:29"), (205, 68, "Debtor", 85800, "2021-02-01 08:04:27"), (344, 38, "Creditor", 14600, "2021-03-05 16:35:02"), (16, 8, "Debtor", 47000, "2021-07-27 14:19:59"), (125, 39, "Debtor", 92600, "2021-12-03 13:31:24"), (339, 26, "Debtor", 5200, "2021-02-28 16:23:31"), (319, 17, "Creditor", 43000, "2021-05-04 17:20:40"), (54, 28, "Debtor", 40100, "2021-11-28 09:39:45"), (328, 52, "Debtor", 62100, "2021-04-27 14:15:05"), (135, 46, "Creditor", 25900, "2021-09-05 15:17:19"), (248, 55, "Creditor", 108800, "2021-08-23 17:13:15"), (368, 51, "Debtor", 16100, "2021-11-04 12:34:05"), (333, 50, "Creditor", 73500, "2021-03-31 11:38:54"), (271, 32, "Debtor", 72100, "2021-08-17 17:22:35"), (250, 28, "Creditor", 108300, "2021-08-06 12:05:32"), (196, 22, "Debtor", 61100, "2021-03-02 17:01:15"), (32, 32, "Debtor", 7500, "2021-02-16 08:08:58"), (132, 42, "Creditor", 26700, "2021-02-06 16:46:46"), (133, 70, "Creditor", 54900, "2021-06-25 11:32:34"), (346, 19, "Creditor", 48800, "2021-09-12 12:24:29"), (105, 74, "Debtor", 23900, "2021-08-31 16:26:05"), (308, 58, "Creditor", 25200, "2021-10-25 15:19:23"), (151, 43, "Debtor", 14200, "2021-01-07 15:19:21"), (6, 22, "Debtor", 84900, "2021-04-01 16:04:50"), (179, 15, "Creditor", 90000, "2021-12-23 16:52:10"), (121, 74, "Debtor", 16800, "2021-01-27 09:19:30"), (305, 38, "Creditor", 7500, "2021-06-14 16:52:03"), (102, 43, "Creditor", 11300, "2021-12-19 08:25:58"), (341, 19, "Creditor", 21000, "2021-12-03 13:06:43"), (358, 26, "Creditor", 31200, "2021-06-26 09:36:45"), (109, 52, "Creditor", 47400, "2021-02-08 14:58:30"), (322, 46, "Creditor", 20600, "2021-02-24 08:50:58"), (316, 26, "Creditor", 31200, "2021-08-23 08:56:03"), (282, 48, "Debtor", 40900, "2021-04-01 13:26:25"), (352, 8, "Creditor", 19900, "2021-02-28 14:56:13"), (98, 46, "Debtor", 89000, "2021-06-20 15:15:48"), (202, 23, "Debtor", 2200, "2021-10-24 15:53:07"), (209, 61, "Debtor", 8500, "2021-11-23 12:52:52"), (78, 46, "Creditor", 13500, "2021-11-10 16:28:41"), (225, 49, "Debtor", 104800, "2021-10-15 08:11:47"), (302, 46, "Creditor", 18800, "2021-06-29 16:15:30"), (72, 38, "Creditor", 4300, "2021-07-24 09:14:28"), (97, 3, "Creditor", 64400, "2021-02-28 08:15:47"), (198, 58, "Debtor", 13100, "2021-09-15 17:44:23"), (15, 51, "Debtor", 3100, "2021-08-19 14:33:04"), (207, 27, "Debtor", 6800, "2021-01-27 16:56:09"), (89, 44, "Debtor", 100000, "2021-10-26 14:12:00"), (115, 46, "Creditor", 49100, "2021-10-07 09:20:30"), (185, 59, "Creditor", 7200, "2021-01-29 10:14:56"), (257, 28, "Creditor", 75100, "2021-01-13 17:20:31"), (169, 3, "Creditor", 91400, "2021-06-23 10:57:49"), (96, 66, "Creditor", 72800, "2021-09-16 17:38:28"), (28, 39, "Creditor", 66800, "2021-10-12 12:55:37"), (366, 15, "Debtor", 41900, "2021-11-24 09:57:29"), (317, 26, "Debtor", 80600, "2021-03-29 14:16:06"), (26, 27, "Creditor", 92900, "2021-04-16 08:39:37"), (314, 49, "Creditor", 70300, "2021-02-22 11:33:48"), (267, 1, "Creditor", 95300, "2021-08-22 11:59:00"), (230, 68, "Creditor", 96600, "2021-04-18 17:01:27"), (99, 66, "Creditor", 99800, "2021-07-15 13:32:53"), (129, 54, "Creditor", 16700, "2021-10-31 16:27:46"), (128, 67, "Creditor", 81800, "2021-01-03 10:13:35"), (330, 8, "Debtor", 109700, "2021-02-20 09:17:28"), (355, 63, "Debtor", 28500, "2021-02-28 17:16:38"), (342, 54, "Creditor", 60000, "2021-07-01 14:16:56"), (3, 15, "Creditor", 75700, "2021-06-03 10:23:11"), (240, 17, "Debtor", 102100, "2021-08-12 11:29:00"), (176, 22, "Debtor", 89200, "2021-09-14 17:22:49"), (70, 48, "Creditor", 79500, "2021-04-22 11:17:45"), (272, 17, "Debtor", 61900, "2021-04-14 11:04:57"), (116, 66, "Debtor", 53600, "2021-03-01 16:13:52"), (83, 70, "Creditor", 39100, "2021-01-09 09:06:57"), (94, 8, "Creditor", 14200, "2021-12-27 13:57:20"), (301, 3, "Debtor", 5600, "2021-03-12 13:45:42"), (274, 48, "Debtor", 11600, "2021-06-18 17:27:52"), (24, 66, "Creditor", 12000, "2021-08-12 15:24:10"), (300, 67, "Creditor", 42400, "2021-11-29 09:45:59"), (223, 43, "Debtor", 30700, "2021-01-22 15:53:52"), (139, 48, "Debtor", 64700, "2021-11-18 08:10:02"), (260, 44, "Creditor", 76500, "2021-06-23 14:24:12"), (233, 19, "Creditor", 72700, "2021-06-15 13:46:53"), (239, 3, "Debtor", 88200, "2021-09-13 08:12:58"), (194, 66, "Debtor", 20700, "2021-02-18 16:51:31"), (159, 17, "Creditor", 86200, "2021-09-09 08:54:35"), (124, 51, "Debtor", 83100, "2021-05-19 08:15:04"), (11, 41, "Creditor", 91400, "2021-08-02 09:55:13"), (323, 17, "Creditor", 71500, "2021-12-11 10:22:38"), (297, 51, "Creditor", 66300, "2021-03-20 17:45:59"), (258, 51, "Creditor", 42700, "2021-09-24 17:42:26"), (63, 38, "Debtor", 96000, "2021-02-26 12:31:30"), (87, 17, "Debtor", 59200, "2021-08-16 16:08:33"), (266, 46, "Creditor", 53400, "2021-01-28 14:56:46"), (74, 50, "Creditor", 21500, "2021-02-09 10:26:30"), (354, 50, "Debtor", 14100, "2021-08-20 14:31:24"), (350, 39, "Debtor", 90800, "2021-12-21 09:08:20"), (338, 35, "Creditor", 44900, "2021-10-01 09:42:20"), (22, 49, "Creditor", 19000, "2021-04-25 09:53:55"), (249, 39, "Debtor", 70400, "2021-09-01 13:09:24"), (195, 58, "Creditor", 1300, "2021-01-30 13:26:45"), (218, 23, "Creditor", 102600, "2021-04-22 16:48:41"), (203, 58, "Creditor", 32200, "2021-05-28 11:10:41"), (278, 74, "Creditor", 53100, "2021-12-27 15:31:50"), (367, 41, "Creditor", 5000, "2021-01-19 15:35:13"), (188, 19, "Creditor", 2600, "2021-06-15 13:48:11"), (19, 19, "Debtor", 52900, "2021-04-18 11:09:32"), (238, 54, "Creditor", 65000, "2021-07-14 10:54:34"), (73, 17, "Creditor", 13300, "2021-10-25 10:01:58"), (212, 55, "Debtor", 26500, "2021-04-15 15:44:54"), (245, 51, "Debtor", 107400, "2021-04-07 17:18:22"), (364, 19, "Creditor", 69800, "2021-03-08 14:11:10"), (14, 59, "Debtor", 9300, "2021-04-26 13:13:51"), (276, 42, "Creditor", 55200, "2021-09-23 10:35:37"), (175, 51, "Debtor", 91500, "2021-05-29 09:04:54"), (275, 51, "Debtor", 30600, "2021-12-15 13:55:17"), (332, 41, "Creditor", 92200, "2021-07-29 14:30:14"), (360, 68, "Debtor", 99400, "2021-01-19 12:23:39"), (291, 74, "Debtor", 15200, "2021-10-28 09:07:18"), (362, 58, "Debtor", 11400, "2021-05-24 13:30:19"), (160, 68, "Debtor", 31800, "2021-04-25 15:24:28"), (111, 3, "Creditor", 107200, "2021-08-20 16:21:16"), (343, 68, "Debtor", 57700, "2021-02-02 11:30:29"), (67, 38, "Creditor", 34200, "2021-12-19 14:41:29"), (163, 28, "Debtor", 29000, "2021-01-14 13:41:22"), (184, 35, "Creditor", 57600, "2021-10-08 10:49:46"), (123, 44, "Creditor", 52500, "2021-05-02 16:32:20"), (148, 49, "Debtor", 52300, "2021-11-19 15:59:30"), (17, 46, "Creditor", 48700, "2021-02-07 13:23:17"), (349, 52, "Creditor", 101600, "2021-06-05 10:11:46"), (268, 58, "Creditor", 41000, "2021-10-16 14:58:15"), (142, 48, "Debtor", 27800, "2021-06-15 13:27:04"), (110, 32, "Debtor", 101400, "2021-12-15 17:02:05"), (88, 42, "Debtor", 27900, "2021-03-11 15:12:13"), (47, 55, "Creditor", 62200, "2021-02-27 11:54:13"), (104, 27, "Debtor", 13200, "2021-03-24 11:58:01"), (120, 8, "Debtor", 81500, "2021-08-11 17:57:11"), (237, 8, "Creditor", 41400, "2021-09-12 13:53:26"), (7, 32, "Creditor", 43200, "2021-01-14 09:09:30"), (95, 58, "Creditor", 92700, "2021-06-13 17:10:07");

SQL2

truncate table accounts;
insert into accounts (account_id, max_income) values
(17, 23000), (15, 48000), (5, 79000), (8, 11000), (1, 89000), (16, 75000), (11, 89000), (9, 103000), (12, 11000), (19, 36000);

truncate table transactions;
insert into transactions (transaction_id, account_id, type, amount, day) values
(60, 16, "Debtor", 82900, "2021-07-02 12:30:44"), (88, 9, "Debtor", 7900, "2021-05-27 13:53:22"), (4, 5, "Debtor", 70700, "2021-07-23 13:58:07"), (51, 17, "Debtor", 50800, "2021-05-15 09:12:25"), (95, 8, "Creditor", 10500, "2021-07-13 11:51:22"), (54, 11, "Debtor", 26000, "2021-06-06 10:48:45"), (30, 9, "Creditor", 22500, "2021-05-16 16:49:55"), (61, 9, "Debtor", 18400, "2021-05-08 15:14:10"), (37, 17, "Creditor", 5900, "2021-07-29 12:12:30"), (84, 17, "Debtor", 6400, "2021-07-10 11:01:50"), (5, 8, "Debtor", 75100, "2021-06-03 16:13:28"), (15, 9, "Debtor", 87300, "2021-05-14 08:07:45"), (3, 5, "Creditor", 36800, "2021-07-01 14:10:15"), (26, 15, "Debtor", 88700, "2021-06-10 09:29:12"), (68, 11, "Debtor", 66600, "2021-05-22 13:16:03"), (48, 5, "Debtor", 77200, "2021-05-07 10:08:45"), (39, 15, "Creditor", 79100, "2021-06-13 14:27:33"), (12, 15, "Creditor", 7300, "2021-05-10 11:18:45"), (38, 5, "Debtor", 108000, "2021-06-22 09:15:08"), (6, 17, "Creditor", 80400, "2021-07-01 16:33:45"), (66, 5, "Creditor", 9500, "2021-05-21 14:15:47"), (81, 12, "Creditor", 22800, "2021-06-17 15:01:57"), (82, 19, "Debtor", 52900, "2021-07-29 13:35:13"), (28, 12, "Creditor", 18100, "2021-07-11 09:02:39"), (70, 8, "Debtor", 28200, "2021-05-07 09:23:24"), (56, 8, "Debtor", 29800, "2021-07-13 12:44:00"), (1, 9, "Creditor", 43700, "2021-07-05 13:38:05"), (31, 17, "Creditor", 84300, "2021-05-20 12:07:40"), (78, 5, "Creditor", 100800, "2021-05-03 15:48:29"), (74, 1, "Creditor", 47300, "2021-06-07 10:31:01"), (59, 12, "Creditor", 55100, "2021-07-31 08:40:46"), (21, 1, "Debtor", 39600, "2021-05-11 16:57:55"), (50, 1, "Debtor", 87200, "2021-05-11 10:38:07"), (92, 12, "Creditor", 80200, "2021-05-15 14:50:10"), (17, 11, "Creditor", 8600, "2021-07-03 14:19:38"), (57, 1, "Creditor", 63000, "2021-05-31 15:52:49"), (29, 15, "Creditor", 50600, "2021-07-26 11:10:19"), (69, 17, "Creditor", 80900, "2021-05-24 11:22:25"), (73, 16, "Creditor", 105000, "2021-07-29 10:04:56"), (71, 19, "Debtor", 73900, "2021-07-07 08:36:36"), (72, 19, "Debtor", 65300, "2021-06-14 15:45:43"), (13, 12, "Creditor", 53000, "2021-07-09 16:26:49"), (98, 1, "Creditor", 64300, "2021-05-10 10:45:59"), (79, 9, "Creditor", 11000, "2021-06-11 14:07:14"), (23, 11, "Debtor", 103400, "2021-07-01 13:46:40"), (7, 12, "Creditor", 77700, "2021-07-17 12:03:12"), (91, 12, "Debtor", 700, "2021-05-26 09:35:24"), (75, 1, "Debtor", 26800, "2021-05-27 17:43:11"), (10, 11, "Debtor", 40600, "2021-05-05 15:24:45"), (16, 9, "Debtor", 22800, "2021-07-23 14:51:36");

SQL3

truncate table accounts;
insert into accounts (account_id, max_income) values
(15, 12000), (2, 3000), (4, 32000), (16, 7000), (9, 19000), (10, 49000), (3, 58000), (14, 18000);

truncate table transactions;
insert into transactions (transaction_id, account_id, type, amount, day) values
(58, 9, "Debtor", 34500, "2021-06-18 17:52:31"), (66, 2, "Creditor", 12100, "2021-05-07 17:14:09"), (43, 15, "Debtor", 79600, "2021-06-19 13:09:17"), (9, 3, "Creditor", 28600, "2021-05-30 11:33:10"), (29, 9, "Debtor", 2700, "2021-07-08 15:25:40"), (70, 2, "Creditor", 104200, "2021-06-07 17:40:02"), (32, 3, "Debtor", 3400, "2021-06-07 14:42:57"), (33, 3, "Creditor", 32700, "2021-05-08 10:16:16"), (25, 3, "Debtor", 37700, "2021-07-19 17:15:39"), (1, 2, "Debtor", 108600, "2021-06-24 09:50:52"), (65, 9, "Debtor", 107000, "2021-05-03 15:37:21"), (38, 15, "Debtor", 2000, "2021-05-17 15:41:58"), (27, 9, "Creditor", 61300, "2021-07-28 13:00:36"), (60, 9, "Debtor", 31800, "2021-06-13 09:33:50"), (40, 10, "Creditor", 81700, "2021-06-18 17:54:06"), (26, 3, "Debtor", 65900, "2021-05-11 17:53:27"), (3, 10, "Creditor", 50400, "2021-06-29 16:12:53"), (42, 16, "Creditor", 94800, "2021-05-27 17:16:32"), (55, 2, "Creditor", 77900, "2021-05-13 13:06:54"), (73, 3, "Debtor", 13000, "2021-06-17 11:18:41"), (54, 15, "Creditor", 24300, "2021-07-29 10:36:49"), (5, 4, "Debtor", 100900, "2021-07-29 13:37:09"), (44, 16, "Creditor", 23100, "2021-07-28 12:54:50"), (15, 10, "Creditor", 29600, "2021-06-18 10:02:07"), (74, 4, "Debtor", 78500, "2021-06-21 09:31:52"), (78, 2, "Creditor", 30300, "2021-07-22 09:49:31"), (64, 16, "Creditor", 90300, "2021-07-26 12:36:27"), (57, 9, "Debtor", 8600, "2021-05-13 12:19:53"), (53, 15, "Creditor", 13300, "2021-06-15 14:26:01"), (79, 16, "Debtor", 74000, "2021-05-29 09:16:28"), (46, 2, "Creditor", 96000, "2021-07-02 14:44:38"), (52, 3, "Creditor", 39800, "2021-07-23 15:31:01"), (51, 3, "Creditor", 55500, "2021-07-10 14:34:32"), (49, 15, "Debtor", 85200, "2021-06-18 12:52:55"), (12, 14, "Creditor", 106100, "2021-05-03 13:25:18"), (16, 9, "Debtor", 106000, "2021-05-21 09:39:47"), (56, 4, "Debtor", 96900, "2021-06-07 16:37:55"), (37, 9, "Creditor", 63300, "2021-06-19 17:04:54"), (45, 4, "Creditor", 77000, "2021-05-28 14:00:42"), (48, 2, "Creditor", 3900, "2021-07-01 16:15:16");
@astak16 astak16 added the 中等 label Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant