返回介绍

solution / 2400-2499 / 2474.Customers With Strictly Increasing Purchases / README_EN

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

2474. Customers With Strictly Increasing Purchases

中文文档

Description

Table: Orders

+--------------+------+
| Column Name  | Type |
+--------------+------+
| order_id   | int  |
| customer_id  | int  |
| order_date   | date |
| price    | int  |
+--------------+------+
order_id is the column with unique values for this table.
Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.

 

Write a solution to report the IDs of the customers with the total purchases strictly increasing yearly.

  • The total purchases of a customer in one year is the sum of the prices of their orders in that year. If for some year the customer did not make any order, we consider the total purchases 0.
  • The first year to consider for each customer is the year of their first order.
  • The last year to consider for each customer is the year of their last order.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Orders table:
+----------+-------------+------------+-------+
| order_id | customer_id | order_date | price |
+----------+-------------+------------+-------+
| 1    | 1       | 2019-07-01 | 1100  |
| 2    | 1       | 2019-11-01 | 1200  |
| 3    | 1       | 2020-05-26 | 3000  |
| 4    | 1       | 2021-08-31 | 3100  |
| 5    | 1       | 2022-12-07 | 4700  |
| 6    | 2       | 2015-01-01 | 700   |
| 7    | 2       | 2017-11-07 | 1000  |
| 8    | 3       | 2017-01-01 | 900   |
| 9    | 3       | 2018-11-07 | 900   |
+----------+-------------+------------+-------+
Output: 
+-------------+
| customer_id |
+-------------+
| 1       |
+-------------+
Explanation: 
Customer 1: The first year is 2019 and the last year is 2022
  - 2019: 1100 + 1200 = 2300
  - 2020: 3000
  - 2021: 3100
  - 2022: 4700
  We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer.

Customer 2: The first year is 2015 and the last year is 2017
  - 2015: 700
  - 2016: 0
  - 2017: 1000
  We do not include customer 2 in the answer because the total purchases are not strictly increasing. Note that customer 2 did not make any purchases in 2016.

Customer 3: The first year is 2017, and the last year is 2018
  - 2017: 900
  - 2018: 900
 We do not include customer 3 in the answer because the total purchases are not strictly increasing.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  customer_id
FROM
  (
    SELECT
      customer_id,
      YEAR(order_date),
      SUM(price) AS total,
      YEAR(order_date) - RANK() OVER (
        PARTITION BY customer_id
        ORDER BY SUM(price)
      ) AS rk
    FROM Orders
    GROUP BY customer_id, YEAR(order_date)
  ) AS t
GROUP BY customer_id
HAVING COUNT(DISTINCT rk) = 1;

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

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

发布评论

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