返回介绍

solution / 2900-2999 / 2986.Find Third Transaction / README_EN

发布于 2024-06-17 01:02:58 字数 3567 浏览 0 评论 0 收藏 0

2986. Find Third Transaction

中文文档

Description

Table: Transactions

+------------------+----------+
| Column Name    | Type   |
+------------------+----------+
| user_id      | int    |
| spend      | decimal  |
| transaction_date | datetime |
+------------------+----------+
(user_id, transaction_date) is column of unique values for this table.
This table contains user_id, spend, and transaction_date.

Write a solution to find the third transaction (if they have at least three transactions) of every user, where the spending on the preceding two transactions is lower than the spending on the third transaction.

Return _the result table by _user_id_ in ascending order__._

The result format is in the following example.

 

Example 1:

Input: 
Transactions table:
+---------+--------+---------------------+
| user_id | spend  | transaction_date  | 
+---------+--------+---------------------+
| 1     | 65.56  | 2023-11-18 13:49:42 | 
| 1     | 96.0   | 2023-11-30 02:47:26 |   
| 1     | 7.44   | 2023-11-02 12:15:23 | 
| 1     | 49.78  | 2023-11-12 00:13:46 | 
| 2     | 40.89  | 2023-11-21 04:39:15 |  
| 2     | 100.44 | 2023-11-20 07:39:34 | 
| 3     | 37.33  | 2023-11-03 06:22:02 | 
| 3     | 13.89  | 2023-11-11 16:00:14 | 
| 3     | 7.0  | 2023-11-29 22:32:36 | 
+---------+--------+---------------------+
Output
+---------+-------------------------+------------------------+
| user_id | third_transaction_spend | third_transaction_date | 
+---------+-------------------------+------------------------+
| 1     | 65.56           | 2023-11-18 13:49:42  |  
+---------+-------------------------+------------------------+
Explanation
- For user_id 1, their third transaction occurred on 2023-11-18 at 13:49:42 with an amount of $65.56, surpassing the expenditures of the previous two transactions which were $7.44 on 2023-11-02 at 12:15:23 and $49.78 on 2023-11-12 at 00:13:46. Thus, this third transaction will be included in the output table.
- user_id 2 only has a total of 2 transactions, so there isn't a third transaction to consider.
- For user_id 3, the amount of $7.0 for their third transaction is less than that of the preceding two transactions, so it won't be included.
Output table is ordered by user_id in ascending order.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  T AS (
    SELECT
      *,
      RANK() OVER (
        PARTITION BY user_id
        ORDER BY transaction_date
      ) AS rk,
      spend > (
        LAG(spend) OVER (
          PARTITION BY user_id
          ORDER BY transaction_date
        )
      )
      AND spend > (
        LAG(spend, 2) OVER (
          PARTITION BY user_id
          ORDER BY transaction_date
        )
      ) AS st
    FROM Transactions
  )
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
FROM T
WHERE rk = 3 AND st = 1;

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

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

发布评论

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