|
| 1 | +# [2474. Customers With Strictly Increasing Purchases](https://leetcode.cn/problems/customers-with-strictly-increasing-purchases) |
| 2 | + |
| 3 | +[English Version](/solution/2400-2499/2474.Customers%20With%20Strictly%20Increasing%20Purchases/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Orders</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++--------------+------+ |
| 13 | +| Column Name | Type | |
| 14 | ++--------------+------+ |
| 15 | +| order_id | int | |
| 16 | +| customer_id | int | |
| 17 | +| order_date | date | |
| 18 | +| price | int | |
| 19 | ++--------------+------+ |
| 20 | +order_id is the primary key for this table. |
| 21 | +Each row contains the id of an order, the id of |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p>Write an SQL query to report the IDs of the customers with the <strong>total purchases</strong> strictly increasing yearly.</p> |
| 27 | + |
| 28 | +<ul> |
| 29 | + <li>The <strong>total purchases</strong> 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 <code>0</code>.</li> |
| 30 | + <li>The first year to consider for each customer is the year of their <strong>first order</strong>.</li> |
| 31 | + <li>The last year to consider for each customer is the year of their <strong>last order</strong>.</li> |
| 32 | +</ul> |
| 33 | + |
| 34 | +<p>Return the result table <strong>in any order</strong>.</p> |
| 35 | + |
| 36 | +<p>The query result format is in the following example.</p> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong class="example">Example 1:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input:</strong> |
| 43 | +Orders table: |
| 44 | ++----------+-------------+------------+-------+ |
| 45 | +| order_id | customer_id | order_date | price | |
| 46 | ++----------+-------------+------------+-------+ |
| 47 | +| 1 | 1 | 2019-07-01 | 1100 | |
| 48 | +| 2 | 1 | 2019-11-01 | 1200 | |
| 49 | +| 3 | 1 | 2020-05-26 | 3000 | |
| 50 | +| 4 | 1 | 2021-08-31 | 3100 | |
| 51 | +| 5 | 1 | 2022-12-07 | 4700 | |
| 52 | +| 6 | 2 | 2015-01-01 | 700 | |
| 53 | +| 7 | 2 | 2017-11-07 | 1000 | |
| 54 | +| 8 | 3 | 2017-01-01 | 900 | |
| 55 | +| 9 | 3 | 2018-11-07 | 900 | |
| 56 | ++----------+-------------+------------+-------+ |
| 57 | +<strong>Output:</strong> |
| 58 | ++-------------+ |
| 59 | +| customer_id | |
| 60 | ++-------------+ |
| 61 | +| 1 | |
| 62 | ++-------------+ |
| 63 | +<strong>Explanation:</strong> |
| 64 | +Customer 1: The first year is 2019 and the last year is 2022 |
| 65 | + - 2019: 1100 + 1200 = 2300 |
| 66 | + - 2020: 3000 |
| 67 | + - 2021: 3100 |
| 68 | + - 2022: 4700 |
| 69 | + We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer. |
| 70 | + |
| 71 | +Customer 2: The first year is 2015 and the last year is 2017 |
| 72 | + - 2015: 700 |
| 73 | + - 2016: 0 |
| 74 | + - 2017: 1000 |
| 75 | + 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. |
| 76 | + |
| 77 | +Customer 3: The first year is 2017, and the last year is 2018 |
| 78 | + - 2017: 900 |
| 79 | + - 2018: 900 |
| 80 | + We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer. |
| 81 | +</pre> |
| 82 | + |
| 83 | +## 解法 |
| 84 | + |
| 85 | +<!-- 这里可写通用的实现逻辑 --> |
| 86 | + |
| 87 | +<!-- tabs:start --> |
| 88 | + |
| 89 | +### **SQL** |
| 90 | + |
| 91 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 92 | + |
| 93 | +```sql |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +<!-- tabs:end --> |
0 commit comments