|
| 1 | +# [3118. Friday Purchase III](https://leetcode.cn/problems/friday-purchase-iii) |
| 2 | + |
| 3 | +[English Version](/solution/3100-3199/3118.Friday%20Purchase%20III/README_EN.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## 题目描述 |
| 8 | + |
| 9 | +<!-- 这里写题目描述 --> |
| 10 | + |
| 11 | +<p>Table: <code>Purchases</code></p> |
| 12 | + |
| 13 | +<pre> |
| 14 | ++---------------+------+ |
| 15 | +| Column Name | Type | |
| 16 | ++---------------+------+ |
| 17 | +| user_id | int | |
| 18 | +| purchase_date | date | |
| 19 | +| amount_spend | int | |
| 20 | ++---------------+------+ |
| 21 | +(user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table. |
| 22 | +purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates. |
| 23 | +Each row contains user_id, purchase_date, and amount_spend. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p>Table: <code>Users</code></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | ++-------------+------+ |
| 30 | +| Column Name | Type | |
| 31 | ++-------------+------+ |
| 32 | +| user_id | int | |
| 33 | +| membership | enum | |
| 34 | ++-------------+------+ |
| 35 | +user_id is the primary key for this table. |
| 36 | +membership is an ENUM (category) type of ('Standard', 'Premium', 'VIP'). |
| 37 | +Each row of this table indicates the user_id, membership type. |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p>Write a solution to calculate the <strong>total spending</strong> by <code>Premium</code> and <code>VIP</code> members on <strong>each Friday of every week</strong> in November 2023. If there are <strong>no purchases</strong> on a <strong>particular Friday</strong> by <code>Premium</code> or <code>VIP</code> members, it should be considered as <code>0</code>.</p> |
| 41 | + |
| 42 | +<p>Return <em>the result table</em> <em>ordered by week of the month, and </em><code>membership</code><em> in <strong>ascending</strong> order</em>.</p> |
| 43 | + |
| 44 | +<p>The result format is in the following example.</p> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong class="example">Example:</strong></p> |
| 48 | + |
| 49 | +<div class="example-block"> |
| 50 | +<p><strong>Input:</strong></p> |
| 51 | + |
| 52 | +<p>Purchases table:</p> |
| 53 | + |
| 54 | +<pre class="example-io"> |
| 55 | ++---------+---------------+--------------+ |
| 56 | +| user_id | purchase_date | amount_spend | |
| 57 | ++---------+---------------+--------------+ |
| 58 | +| 11 | 2023-11-03 | 1126 | |
| 59 | +| 15 | 2023-11-10 | 7473 | |
| 60 | +| 17 | 2023-11-17 | 2414 | |
| 61 | +| 12 | 2023-11-24 | 9692 | |
| 62 | +| 8 | 2023-11-24 | 5117 | |
| 63 | +| 1 | 2023-11-24 | 5241 | |
| 64 | +| 10 | 2023-11-22 | 8266 | |
| 65 | +| 13 | 2023-11-21 | 12000 | |
| 66 | ++---------+---------------+--------------+ |
| 67 | +</pre> |
| 68 | + |
| 69 | +<p>Users table:</p> |
| 70 | + |
| 71 | +<pre class="example-io"> |
| 72 | ++---------+------------+ |
| 73 | +| user_id | membership | |
| 74 | ++---------+------------+ |
| 75 | +| 11 | Premium | |
| 76 | +| 15 | VIP | |
| 77 | +| 17 | Standard | |
| 78 | +| 12 | VIP | |
| 79 | +| 8 | Premium | |
| 80 | +| 1 | VIP | |
| 81 | +| 10 | Standard | |
| 82 | +| 13 | Premium | |
| 83 | ++---------+------------+ |
| 84 | +</pre> |
| 85 | + |
| 86 | +<p><strong>Output:</strong></p> |
| 87 | + |
| 88 | +<pre class="example-io"> |
| 89 | ++---------------+-------------+--------------+ |
| 90 | +| week_of_month | membership | total_amount | |
| 91 | ++---------------+-------------+--------------+ |
| 92 | +| 1 | Premium | 1126 | |
| 93 | +| 1 | VIP | 0 | |
| 94 | +| 2 | Premium | 0 | |
| 95 | +| 2 | VIP | 7473 | |
| 96 | +| 3 | Premium | 0 | |
| 97 | +| 3 | VIP | 0 | |
| 98 | +| 4 | Premium | 5117 | |
| 99 | +| 4 | VIP | 14933 | |
| 100 | ++---------------+-------------+--------------+ |
| 101 | + </pre> |
| 102 | + |
| 103 | +<p><strong>Explanation:</strong></p> |
| 104 | + |
| 105 | +<ul> |
| 106 | + <li>During the first week of November 2023, a transaction occurred on Friday, 2023-11-03, by a Premium member amounting to $1,126. No transactions were made by VIP members on this day, resulting in a value of 0.</li> |
| 107 | + <li>For the second week of November 2023, there was a transaction on Friday, 2023-11-10, and it was made by a VIP member, amounting to $7,473. Since there were no purchases by Premium members that Friday, the output shows 0 for Premium members.</li> |
| 108 | + <li>Similarly, during the third week of November 2023, no transactions by Premium or VIP members occurred on Friday, 2023-11-17, which shows 0 for both categories in this week.</li> |
| 109 | + <li>In the fourth week of November 2023, transactions occurred on Friday, 2023-11-24, involving one Premium member purchase of $5,117 and VIP member purchases totaling $14,933 ($9,692 from one and $5,241 from another).</li> |
| 110 | +</ul> |
| 111 | + |
| 112 | +<p><strong>Note:</strong> The output table is ordered by week_of_month and membership in ascending order.</p> |
| 113 | +</div> |
| 114 | + |
| 115 | +## 解法 |
| 116 | + |
| 117 | +### 方法一:递归 + 连接 |
| 118 | + |
| 119 | +我们首先创建一个递归表 `T`,其中包含 `week_of_month` 列,表示月份的第几周。然后创建一个表 `M`,包含 `membership` 列,表示会员类型,取值为 `'Premium'` 和 `'VIP'`。 |
| 120 | + |
| 121 | +接着创建一个表 `P`,包含 `week_of_month`、`membership` 和 `amount_spend` 列,筛选出每个会员在每个月的第几周的周五的消费金额。最后,我们将 `T` 和 `M` 表连接,再左连接 `P` 表,并且按照 `week_of_month` 和 `membership` 列进行分组,计算每周每种会员的总消费金额。 |
| 122 | + |
| 123 | +<!-- tabs:start --> |
| 124 | + |
| 125 | +```sql |
| 126 | +# Write your MySQL query statement below |
| 127 | +WITH RECURSIVE |
| 128 | + T AS ( |
| 129 | + SELECT 1 AS week_of_month |
| 130 | + UNION |
| 131 | + SELECT week_of_month + 1 |
| 132 | + FROM T |
| 133 | + WHERE week_of_month < 4 |
| 134 | + ), |
| 135 | + M AS ( |
| 136 | + SELECT 'Premium' AS membership |
| 137 | + UNION |
| 138 | + SELECT 'VIP' |
| 139 | + ), |
| 140 | + P AS ( |
| 141 | + SELECT CEIL(DAYOFMONTH(purchase_date) / 7) AS week_of_month, membership, amount_spend |
| 142 | + FROM |
| 143 | + Purchases |
| 144 | + JOIN Users USING (user_id) |
| 145 | + WHERE DAYOFWEEK(purchase_date) = 6 |
| 146 | + ) |
| 147 | +SELECT week_of_month, membership, IFNULL(SUM(amount_spend), 0) AS total_amount |
| 148 | +FROM |
| 149 | + T |
| 150 | + JOIN M |
| 151 | + LEFT JOIN P USING (week_of_month, membership) |
| 152 | +GROUP BY 1, 2 |
| 153 | +ORDER BY 1, 2; |
| 154 | +``` |
| 155 | + |
| 156 | +<!-- tabs:end --> |
| 157 | + |
| 158 | +<!-- end --> |
0 commit comments