返回介绍

solution / 1800-1899 / 1843.Suspicious Bank Accounts / README_EN

发布于 2024-06-17 01:03:14 字数 5304 浏览 0 评论 0 收藏 0

1843. Suspicious Bank Accounts

中文文档

Description

Table: Accounts

+----------------+------+
| Column Name  | Type |
+----------------+------+
| account_id   | int  |
| max_income   | int  |
+----------------+------+
account_id is the column with unique values for this table.
Each row contains information about the maximum monthly income for one bank account.

 

Table: Transactions

+----------------+----------+
| Column Name  | Type   |
+----------------+----------+
| transaction_id | int    |
| account_id   | int    |
| type       | ENUM   |
| amount     | int    |
| day      | datetime |
+----------------+----------+
transaction_id is the column with unique values for this table.
Each row contains information about one transaction.
type is ENUM (category) type of ('Creditor','Debtor') where 'Creditor' means the user deposited money into their account and 'Debtor' means the user withdrew money from their account.
amount is the amount of money deposited/withdrawn during the transaction.

 

A bank account is suspicious if the total income exceeds the max_income for this account for two or more consecutive months. The total income of an account in some month is the sum of all its deposits in that month (i.e., transactions of the type 'Creditor').

Write a solution to report the IDs of all suspicious bank accounts.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Accounts table:
+------------+------------+
| account_id | max_income |
+------------+------------+
| 3      | 21000    |
| 4      | 10400    |
+------------+------------+
Transactions table:
+----------------+------------+----------+--------+---------------------+
| 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 |
+----------------+------------+----------+--------+---------------------+
Output: 
+------------+
| account_id |
+------------+
| 3      |
+------------+
Explanation: 
For account 3:
- In 6-2021, the user had an income of 107100 + 102100 + 90900 = 300100.
- In 7-2021, the user had an income of 64900.
We can see that the income exceeded the max income of 21000 for two consecutive months, so we include 3 in the result table.

For account 4:
- In 5-2021, the user had an income of 49300.
- In 6-2021, the user had an income of 10400.
- In 7-2021, the user had an income of 56300.
We can see that the income exceeded the max income in May and July, but not in June. Since the account did not exceed the max income for two consecutive months, we do not include it in the result table.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  S AS (
    SELECT DISTINCT
      t.account_id,
      DATE_FORMAT(day, '%Y-%m-01') AS day,
      transaction_id AS tx,
      SUM(amount) OVER (
        PARTITION BY account_id, DATE_FORMAT(day, '%Y-%m-01')
      ) > max_income AS marked
    FROM
      Transactions AS t
      LEFT JOIN Accounts AS a ON t.account_id = a.account_id
    WHERE type = 'Creditor'
  )
SELECT DISTINCT s1.account_id
FROM
  S AS s1
  LEFT JOIN S AS s2 ON s1.account_id = s2.account_id AND TIMESTAMPDIFF(Month, s1.day, s2.day) = 1
WHERE s1.marked = 1 AND s2.marked = 1
ORDER BY s1.tx;

Solution 2

# Write your MySQL query statement below
WITH
  S AS (
    SELECT
      account_id,
      DATE_FORMAT(day, '%Y%m') AS yearmonth,
      transaction_id AS tx
    FROM
      Transactions
      JOIN Accounts USING (account_id)
    WHERE type = 'Creditor'
    GROUP BY account_id, yearmonth
    HAVING SUM(amount) > AVG(max_income)
  )
SELECT DISTINCT account_id
FROM S
WHERE (account_id, PERIOD_ADD(yearmonth, 1)) IN (SELECT account_id, yearmonth FROM S)
ORDER BY tx;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文