From bb5e9d72bcbdf06a5146a4d5b4d218f35ca21d2c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 22 Jul 2024 09:24:04 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3230 No.3230.Customer Purchasing Behavior Analysis --- .../README.md | 202 ++++++++++++++++++ .../README_EN.md | 202 ++++++++++++++++++ .../Solution.sql | 40 ++++ solution/DATABASE_README.md | 1 + solution/DATABASE_README_EN.md | 1 + solution/README.md | 1 + solution/README_EN.md | 1 + 7 files changed, 448 insertions(+) create mode 100644 solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md create mode 100644 solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md create mode 100644 solution/3200-3299/3230.Customer Purchasing Behavior Analysis/Solution.sql diff --git a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md new file mode 100644 index 0000000000000..675787b9cac98 --- /dev/null +++ b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md @@ -0,0 +1,202 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md +--- + + + +# [3230. Customer Purchasing Behavior Analysis 🔒](https://leetcode.cn/problems/customer-purchasing-behavior-analysis) + +[English Version](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) + +## 题目描述 + + + +

Table: Transactions

+ +
++------------------+---------+
+| Column Name      | Type    |
++------------------+---------+
+| transaction_id   | int     |
+| customer_id      | int     |
+| product_id       | int     |
+| transaction_date | date    |
+| amount           | decimal |
++------------------+---------+
+transaction_id is the unique identifier for this table.
+Each row of this table contains information about a transaction, including the customer ID, product ID, date, and amount spent.
+
+ +

Table: Products

+ +
++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| product_id  | int     |
+| category    | varchar |
+| price       | decimal |
++-------------+---------+
+product_id is the unique identifier for this table.
+Each row of this table contains information about a product, including its category and price.
+
+ +

Write a solution to analyze customer purchasing behavior. For each customer, calculate:

+ + + +

Round total_amount, avg_transaction_amount, and loyalty_score to 2 decimal places.

+ +

Return the result table ordered by loyalty_score in descending order, then by customer_id in ascending order.

+ +

The query result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

Transactions table:

+ +
++----------------+-------------+------------+------------------+--------+
+| transaction_id | customer_id | product_id | transaction_date | amount |
++----------------+-------------+------------+------------------+--------+
+| 1              | 101         | 1          | 2023-01-01       | 100.00 |
+| 2              | 101         | 2          | 2023-01-15       | 150.00 |
+| 3              | 102         | 1          | 2023-01-01       | 100.00 |
+| 4              | 102         | 3          | 2023-01-22       | 200.00 |
+| 5              | 101         | 3          | 2023-02-10       | 200.00 |
++----------------+-------------+------------+------------------+--------+
+
+ +

Products table:

+ +
++------------+----------+--------+
+| product_id | category | price  |
++------------+----------+--------+
+| 1          | A        | 100.00 |
+| 2          | B        | 150.00 |
+| 3          | C        | 200.00 |
++------------+----------+--------+
+
+ +

Output:

+ +
++-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
+| customer_id | total_amount | transaction_count | unique_categories | avg_transaction_amount | top_category | loyalty_score |
++-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
+| 101         | 450.00       | 3                 | 3                 | 150.00                 | C            | 34.50         |
+| 102         | 300.00       | 2                 | 2                 | 150.00                 | C            | 23.00         |
++-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
+
+ +

Explanation:

+ + + +

Note: The output is ordered by loyalty_score in descending order, then by customer_id in ascending order.

+
+ + + +## 解法 + + + +### 方法一:分组 + 窗口函数 + 连接 + +我们首先将 `Transactions` 表和 `Products` 表连接起来,记录在临时表 `T` 中。 + +然后,我们使用 `T` 表计算每个用户在每个类别下的交易次数以及最近的交易日期,将结果保存在临时表 `P` 中。 + +接着,我们使用 `P` 表计算每个用户在每个类别下的交易次数的排名,将结果保存在临时表 `R` 中。 + +最后,我们使用 `T` 表和 `R` 表计算每个用户的总交易金额、交易次数、唯一类别数、平均交易金额、最常购买的类别、忠诚度分数,并按照忠诚度分数降序、用户 ID 升序的顺序返回结果。 + + + +#### MySQL + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT * + FROM + Transactions + JOIN Products USING (product_id) + ), + P AS ( + SELECT + customer_id, + category, + COUNT(1) cnt, + MAX(transaction_date) max_date + FROM T + GROUP BY 1, 2 + ), + R AS ( + SELECT + customer_id, + category, + RANK() OVER ( + PARTITION BY customer_id + ORDER BY cnt DESC, max_date DESC + ) rk + FROM P + ) +SELECT + t.customer_id, + ROUND(SUM(amount), 2) total_amount, + COUNT(1) transaction_count, + COUNT(DISTINCT t.category) unique_categories, + ROUND(AVG(amount), 2) avg_transaction_amount, + r.category top_category, + ROUND(COUNT(1) * 10 + SUM(amount) / 100, 2) loyalty_score +FROM + T t + JOIN R r ON t.customer_id = r.customer_id AND r.rk = 1 +GROUP BY 1 +ORDER BY 7 DESC, 1; +``` + + + + + + diff --git a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md new file mode 100644 index 0000000000000..6b9f903ab05e6 --- /dev/null +++ b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md @@ -0,0 +1,202 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md +--- + + + +# [3230. Customer Purchasing Behavior Analysis 🔒](https://leetcode.com/problems/customer-purchasing-behavior-analysis) + +[中文文档](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) + +## Description + + + +

Table: Transactions

+ +
++------------------+---------+
+| Column Name      | Type    |
++------------------+---------+
+| transaction_id   | int     |
+| customer_id      | int     |
+| product_id       | int     |
+| transaction_date | date    |
+| amount           | decimal |
++------------------+---------+
+transaction_id is the unique identifier for this table.
+Each row of this table contains information about a transaction, including the customer ID, product ID, date, and amount spent.
+
+ +

Table: Products

+ +
++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| product_id  | int     |
+| category    | varchar |
+| price       | decimal |
++-------------+---------+
+product_id is the unique identifier for this table.
+Each row of this table contains information about a product, including its category and price.
+
+ +

Write a solution to analyze customer purchasing behavior. For each customer, calculate:

+ + + +

Round total_amount, avg_transaction_amount, and loyalty_score to 2 decimal places.

+ +

Return the result table ordered by loyalty_score in descending order, then by customer_id in ascending order.

+ +

The query result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

Transactions table:

+ +
++----------------+-------------+------------+------------------+--------+
+| transaction_id | customer_id | product_id | transaction_date | amount |
++----------------+-------------+------------+------------------+--------+
+| 1              | 101         | 1          | 2023-01-01       | 100.00 |
+| 2              | 101         | 2          | 2023-01-15       | 150.00 |
+| 3              | 102         | 1          | 2023-01-01       | 100.00 |
+| 4              | 102         | 3          | 2023-01-22       | 200.00 |
+| 5              | 101         | 3          | 2023-02-10       | 200.00 |
++----------------+-------------+------------+------------------+--------+
+
+ +

Products table:

+ +
++------------+----------+--------+
+| product_id | category | price  |
++------------+----------+--------+
+| 1          | A        | 100.00 |
+| 2          | B        | 150.00 |
+| 3          | C        | 200.00 |
++------------+----------+--------+
+
+ +

Output:

+ +
++-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
+| customer_id | total_amount | transaction_count | unique_categories | avg_transaction_amount | top_category | loyalty_score |
++-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
+| 101         | 450.00       | 3                 | 3                 | 150.00                 | C            | 34.50         |
+| 102         | 300.00       | 2                 | 2                 | 150.00                 | C            | 23.00         |
++-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
+
+ +

Explanation:

+ + + +

Note: The output is ordered by loyalty_score in descending order, then by customer_id in ascending order.

+
+ + + +## Solutions + + + +### Solution 1: Grouping + Window Functions + Join + +First, we join the `Transactions` table with the `Products` table, recording the result in a temporary table `T`. + +Next, we use the `T` table to calculate the transaction count and the most recent transaction date for each user in each category, saving the results in a temporary table `P`. + +Then, we use the `P` table to calculate the ranking of transaction counts for each user in each category, saving the results in a temporary table `R`. + +Finally, we use the `T` and `R` tables to calculate the total transaction amount, transaction count, unique category count, average transaction amount, most frequently purchased category, and loyalty score for each user, and return the results in descending order of loyalty score and ascending order of user ID. + + + +#### MySQL + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT * + FROM + Transactions + JOIN Products USING (product_id) + ), + P AS ( + SELECT + customer_id, + category, + COUNT(1) cnt, + MAX(transaction_date) max_date + FROM T + GROUP BY 1, 2 + ), + R AS ( + SELECT + customer_id, + category, + RANK() OVER ( + PARTITION BY customer_id + ORDER BY cnt DESC, max_date DESC + ) rk + FROM P + ) +SELECT + t.customer_id, + ROUND(SUM(amount), 2) total_amount, + COUNT(1) transaction_count, + COUNT(DISTINCT t.category) unique_categories, + ROUND(AVG(amount), 2) avg_transaction_amount, + r.category top_category, + ROUND(COUNT(1) * 10 + SUM(amount) / 100, 2) loyalty_score +FROM + T t + JOIN R r ON t.customer_id = r.customer_id AND r.rk = 1 +GROUP BY 1 +ORDER BY 7 DESC, 1; +``` + + + + + + diff --git a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/Solution.sql b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/Solution.sql new file mode 100644 index 0000000000000..0b4b216327dd9 --- /dev/null +++ b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/Solution.sql @@ -0,0 +1,40 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT * + FROM + Transactions + JOIN Products USING (product_id) + ), + P AS ( + SELECT + customer_id, + category, + COUNT(1) cnt, + MAX(transaction_date) max_date + FROM T + GROUP BY 1, 2 + ), + R AS ( + SELECT + customer_id, + category, + RANK() OVER ( + PARTITION BY customer_id + ORDER BY cnt DESC, max_date DESC + ) rk + FROM P + ) +SELECT + t.customer_id, + ROUND(SUM(amount), 2) total_amount, + COUNT(1) transaction_count, + COUNT(DISTINCT t.category) unique_categories, + ROUND(AVG(amount), 2) avg_transaction_amount, + r.category top_category, + ROUND(COUNT(1) * 10 + SUM(amount) / 100, 2) loyalty_score +FROM + T t + JOIN R r ON t.customer_id = r.customer_id AND r.rk = 1 +GROUP BY 1 +ORDER BY 7 DESC, 1; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 5e07c7b269103..c1c577078f022 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -288,6 +288,7 @@ | 3204 | [按位用户权限分析](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README.md) | `数据库` | 中等 | 🔒 | | 3214 | [同比增长率](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 | | 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | | 中等 | | +| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 498c7ac873005..60642b1f58123 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -286,6 +286,7 @@ Press Control + F(or Command + F on | 3204 | [Bitwise User Permissions Analysis](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | | 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 | | 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | | Medium | | +| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index f3a3b15b31244..9d390d61b487c 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3240,6 +3240,7 @@ | 3227 | [字符串元音游戏](/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README.md) | | 中等 | 第 407 场周赛 | | 3228 | [将 1 移动到末尾的最大操作次数](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README.md) | | 中等 | 第 407 场周赛 | | 3229 | [使数组等于目标数组所需的最少操作次数](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md) | | 困难 | 第 407 场周赛 | +| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index a666a7512637d..54c4f82026a54 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3238,6 +3238,7 @@ Press Control + F(or Command + F on | 3227 | [Vowels Game in a String](/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README_EN.md) | | Medium | Weekly Contest 407 | | 3228 | [Maximum Number of Operations to Move Ones to the End](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README_EN.md) | | Medium | Weekly Contest 407 | | 3229 | [Minimum Operations to Make Array Equal to Target](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md) | | Hard | Weekly Contest 407 | +| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | | Medium | 🔒 | ## Copyright