{ "data": { "question": { "questionId": "4025", "questionFrontendId": "3657", "categoryTitle": "Database", "boundTopicId": 3761718, "title": "Find Loyal Customers", "titleSlug": "find-loyal-customers", "content": "
Table: customer_transactions
\n+------------------+---------+\n| Column Name | Type | \n+------------------+---------+\n| transaction_id | int |\n| customer_id | int |\n| transaction_date | date |\n| amount | decimal |\n| transaction_type | varchar |\n+------------------+---------+\ntransaction_id is the unique identifier for this table.\ntransaction_type can be either 'purchase' or 'refund'.\n\n\n
Write a solution to find loyal customers. A customer is considered loyal if they meet ALL the following criteria:
\n\n3
purchase transactions.30
days.20%
.Return the result table ordered by customer_id
in ascending order.
The result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\ncustomer_transactions table:
\n\n\n+----------------+-------------+------------------+--------+------------------+\n| transaction_id | customer_id | transaction_date | amount | transaction_type |\n+----------------+-------------+------------------+--------+------------------+\n| 1 | 101 | 2024-01-05 | 150.00 | purchase |\n| 2 | 101 | 2024-01-15 | 200.00 | purchase |\n| 3 | 101 | 2024-02-10 | 180.00 | purchase |\n| 4 | 101 | 2024-02-20 | 250.00 | purchase |\n| 5 | 102 | 2024-01-10 | 100.00 | purchase |\n| 6 | 102 | 2024-01-12 | 120.00 | purchase |\n| 7 | 102 | 2024-01-15 | 80.00 | refund |\n| 8 | 102 | 2024-01-18 | 90.00 | refund |\n| 9 | 102 | 2024-02-15 | 130.00 | purchase |\n| 10 | 103 | 2024-01-01 | 500.00 | purchase |\n| 11 | 103 | 2024-01-02 | 450.00 | purchase |\n| 12 | 103 | 2024-01-03 | 400.00 | purchase |\n| 13 | 104 | 2024-01-01 | 200.00 | purchase |\n| 14 | 104 | 2024-02-01 | 250.00 | purchase |\n| 15 | 104 | 2024-02-15 | 300.00 | purchase |\n| 16 | 104 | 2024-03-01 | 350.00 | purchase |\n| 17 | 104 | 2024-03-10 | 280.00 | purchase |\n| 18 | 104 | 2024-03-15 | 100.00 | refund |\n+----------------+-------------+------------------+--------+------------------+\n\n\n
Output:
\n\n\n+-------------+\n| customer_id |\n+-------------+\n| 101 |\n| 104 |\n+-------------+\n\n\n
Explanation:
\n\nThe result table is ordered by customer_id in ascending order.
\n表:customer_transactions
\n+------------------+---------+\n| Column Name | Type | \n+------------------+---------+\n| transaction_id | int |\n| customer_id | int |\n| transaction_date | date |\n| amount | decimal |\n| transaction_type | varchar |\n+------------------+---------+\ntransaction_id 是这张表的唯一主键。\ntransaction_type 可以是 “purchase” 或 “refund”。\n\n\n
编写一个解决方案来查找 忠实客户。如果满足下述所有条件,可以认为该客户是 忠实 客户:
\n\n3
次购买交易。30
天。20%
。返回结果表以 customer_id
升序 排序。
结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\ncustomer_transactions 表:
\n\n\n+----------------+-------------+------------------+--------+------------------+\n| transaction_id | customer_id | transaction_date | amount | transaction_type |\n+----------------+-------------+------------------+--------+------------------+\n| 1 | 101 | 2024-01-05 | 150.00 | purchase |\n| 2 | 101 | 2024-01-15 | 200.00 | purchase |\n| 3 | 101 | 2024-02-10 | 180.00 | purchase |\n| 4 | 101 | 2024-02-20 | 250.00 | purchase |\n| 5 | 102 | 2024-01-10 | 100.00 | purchase |\n| 6 | 102 | 2024-01-12 | 120.00 | purchase |\n| 7 | 102 | 2024-01-15 | 80.00 | refund |\n| 8 | 102 | 2024-01-18 | 90.00 | refund |\n| 9 | 102 | 2024-02-15 | 130.00 | purchase |\n| 10 | 103 | 2024-01-01 | 500.00 | purchase |\n| 11 | 103 | 2024-01-02 | 450.00 | purchase |\n| 12 | 103 | 2024-01-03 | 400.00 | purchase |\n| 13 | 104 | 2024-01-01 | 200.00 | purchase |\n| 14 | 104 | 2024-02-01 | 250.00 | purchase |\n| 15 | 104 | 2024-02-15 | 300.00 | purchase |\n| 16 | 104 | 2024-03-01 | 350.00 | purchase |\n| 17 | 104 | 2024-03-10 | 280.00 | purchase |\n| 18 | 104 | 2024-03-15 | 100.00 | refund |\n+----------------+-------------+------------------+--------+------------------+\n\n\n
输出:
\n\n\n+-------------+\n| customer_id |\n+-------------+\n| 101 |\n| 104 |\n+-------------+\n\n\n
解释:
\n\n结果表以 customer_id 升序排序。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"customer_transactions\":[\"transaction_id\",\"customer_id\",\"transaction_date\",\"amount\",\"transaction_type\"]},\"rows\":{\"customer_transactions\":[[1,101,\"2024-01-05\",150.00,\"purchase\"],[2,101,\"2024-01-15\",200.00,\"purchase\"],[3,101,\"2024-02-10\",180.00,\"purchase\"],[4,101,\"2024-02-20\",250.00,\"purchase\"],[5,102,\"2024-01-10\",100.00,\"purchase\"],[6,102,\"2024-01-12\",120.00,\"purchase\"],[7,102,\"2024-01-15\",80.00,\"refund\"],[8,102,\"2024-01-18\",90.00,\"refund\"],[9,102,\"2024-02-15\",130.00,\"purchase\"],[10,103,\"2024-01-01\",500.00,\"purchase\"],[11,103,\"2024-01-02\",450.00,\"purchase\"],[12,103,\"2024-01-03\",400.00,\"purchase\"],[13,104,\"2024-01-01\",200.00,\"purchase\"],[14,104,\"2024-02-01\",250.00,\"purchase\"],[15,104,\"2024-02-15\",300.00,\"purchase\"],[16,104,\"2024-03-01\",350.00,\"purchase\"],[17,104,\"2024-03-10\",280.00,\"purchase\"],[18,104,\"2024-03-15\",100.00,\"refund\"]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"