mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 07:51:41 +08:00
98 lines
20 KiB
JSON
98 lines
20 KiB
JSON
{
|
||
"data": {
|
||
"question": {
|
||
"questionId": "4025",
|
||
"questionFrontendId": "3657",
|
||
"categoryTitle": "Database",
|
||
"boundTopicId": 3761718,
|
||
"title": "Find Loyal Customers",
|
||
"titleSlug": "find-loyal-customers",
|
||
"content": "<p>Table: <code>customer_transactions</code></p>\n\n<pre>\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</pre>\n\n<p>Write a solution to find <strong>loyal customers</strong>. A customer is considered <strong>loyal</strong> if they meet ALL the following criteria:</p>\n\n<ul>\n\t<li>Made <strong>at least</strong> <code><font face=\"monospace\">3</font></code> purchase transactions.</li>\n\t<li>Have been active for <strong>at least</strong> <code>30</code> days.</li>\n\t<li>Their <strong>refund rate</strong> is less than <code>20%</code> .</li>\n</ul>\n\n<p>Return <em>the result table ordered by</em> <code>customer_id</code> <em>in <strong>ascending</strong> order</em>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong></p>\n\n<p>customer_transactions table:</p>\n\n<pre class=\"example-io\">\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</pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+\n| customer_id |\n+-------------+\n| 101 |\n| 104 |\n+-------------+\n</pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li><strong>Customer 101</strong>:\n\n\t<ul>\n\t\t<li>Purchase transactions: 4 (IDs: 1, 2, 3, 4) </li>\n\t\t<li>Refund transactions: 0</li>\n\t\t<li>Refund rate: 0/4 = 0% (less than 20%) </li>\n\t\t<li>Active period: Jan 5 to Feb 20 = 46 days (at least 30 days) </li>\n\t\t<li>Qualifies as loyal </li>\n\t</ul>\n\t</li>\n\t<li><strong>Customer 102</strong>:\n\t<ul>\n\t\t<li>Purchase transactions: 3 (IDs: 5, 6, 9) </li>\n\t\t<li>Refund transactions: 2 (IDs: 7, 8)</li>\n\t\t<li>Refund rate: 2/5 = 40% (exceeds 20%) </li>\n\t\t<li>Not loyal </li>\n\t</ul>\n\t</li>\n\t<li><strong>Customer 103</strong>:\n\t<ul>\n\t\t<li>Purchase transactions: 3 (IDs: 10, 11, 12) </li>\n\t\t<li>Refund transactions: 0</li>\n\t\t<li>Refund rate: 0/3 = 0% (less than 20%) </li>\n\t\t<li>Active period: Jan 1 to Jan 3 = 2 days (less than 30 days) </li>\n\t\t<li>Not loyal </li>\n\t</ul>\n\t</li>\n\t<li><strong>Customer 104</strong>:\n\t<ul>\n\t\t<li>Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17) </li>\n\t\t<li>Refund transactions: 1 (ID: 18)</li>\n\t\t<li>Refund rate: 1/6 = 16.67% (less than 20%) </li>\n\t\t<li>Active period: Jan 1 to Mar 15 = 73 days (at least 30 days) </li>\n\t\t<li>Qualifies as loyal </li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>The result table is ordered by customer_id in ascending order.</p>\n</div>\n",
|
||
"translatedTitle": "寻找忠实客户",
|
||
"translatedContent": "<p>表:<code>customer_transactions</code></p>\n\n<pre>\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</pre>\n\n<p>编写一个解决方案来查找 <strong>忠实客户</strong>。如果满足下述所有条件,可以认为该客户是 <strong>忠实</strong> 客户:</p>\n\n<ul>\n\t<li>进行了 <strong>至少</strong> <code><font face=\"monospace\">3</font></code> 次购买交易。</li>\n\t<li>活跃了 <strong>至少</strong> <code>30</code> 天。</li>\n\t<li>他们的 <strong>退款率</strong> 少于 <code>20%</code>。</li>\n</ul>\n\n<p>返回结果表以 <code>customer_id</code> <strong>升序</strong> 排序。</p>\n\n<p>结果格式如下所示。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>输入:</strong></p>\n\n<p>customer_transactions 表:</p>\n\n<pre class=\"example-io\">\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</pre>\n\n<p><strong>输出:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+\n| customer_id |\n+-------------+\n| 101 |\n| 104 |\n+-------------+\n</pre>\n\n<p><strong>解释:</strong></p>\n\n<ul>\n\t<li><strong>客户 101</strong>:\n\n\t<ul>\n\t\t<li>购买交易:4 (IDs: 1, 2, 3, 4) </li>\n\t\t<li>退款交易:0</li>\n\t\t<li>退款率:0/4 = 0%(少于 20%)</li>\n\t\t<li>活跃时期:1 月 5 日到 2 月 20 日 = 46 天(至少 30 天)</li>\n\t\t<li>符合忠诚客户条件</li>\n\t</ul>\n\t</li>\n\t<li><strong>客户 102</strong>:\n\t<ul>\n\t\t<li>购买交易:3 (IDs: 5, 6, 9) </li>\n\t\t<li>退款交易:2 (IDs: 7, 8)</li>\n\t\t<li>退款率:2/5 = 40% (超过 20%) </li>\n\t\t<li>不符合忠诚客户条件</li>\n\t</ul>\n\t</li>\n\t<li><strong>客户 103</strong>:\n\t<ul>\n\t\t<li>购买交易:3 (IDs: 10, 11, 12) </li>\n\t\t<li>退款交易:0</li>\n\t\t<li>退款率:0/3 = 0%(少于 20%)</li>\n\t\t<li>活跃时期:1 月 1 日到 1 月 3 日 = 2 天(少于 30 天)</li>\n\t\t<li>不符合忠诚客户条件</li>\n\t</ul>\n\t</li>\n\t<li><strong>客户 104</strong>:\n\t<ul>\n\t\t<li>购买交易:5 (IDs: 13, 14, 15, 16, 17) </li>\n\t\t<li>退款交易:1 (ID: 18)</li>\n\t\t<li>退款率:1/6 = 16.67%(少于 20%)</li>\n\t\t<li>活跃时期:1 月 1 日到 3 月 15 日 = 73 天(至少 30 天)</li>\n\t\t<li>符合忠诚客户条件</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>结果表以 customer_id 升序排序。</p>\n</div>\n",
|
||
"isPaidOnly": false,
|
||
"difficulty": "Medium",
|
||
"likes": 1,
|
||
"dislikes": 0,
|
||
"isLiked": null,
|
||
"similarQuestions": "[]",
|
||
"contributors": [],
|
||
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}",
|
||
"topicTags": [],
|
||
"companyTagStats": null,
|
||
"codeSnippets": [
|
||
{
|
||
"lang": "MySQL",
|
||
"langSlug": "mysql",
|
||
"code": "# Write your MySQL query statement below",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "MS SQL Server",
|
||
"langSlug": "mssql",
|
||
"code": "/* Write your T-SQL query statement below */",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "Oracle",
|
||
"langSlug": "oraclesql",
|
||
"code": "/* Write your PL/SQL query statement below */",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "Pandas",
|
||
"langSlug": "pythondata",
|
||
"code": "import pandas as pd\n\ndef find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:\n ",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "PostgreSQL",
|
||
"langSlug": "postgresql",
|
||
"code": "-- Write your PostgreSQL query statement below",
|
||
"__typename": "CodeSnippetNode"
|
||
}
|
||
],
|
||
"stats": "{\"totalAccepted\": \"216\", \"totalSubmission\": \"318\", \"totalAcceptedRaw\": 216, \"totalSubmissionRaw\": 318, \"acRate\": \"67.9%\"}",
|
||
"hints": [],
|
||
"solution": null,
|
||
"status": null,
|
||
"sampleTestCase": "{\"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\"]]}}",
|
||
"metaData": "{\"mysql\":[\"CREATE TABLE if not exists customer_transactions (\\n transaction_id INT,\\n customer_id INT,\\n transaction_date DATE,\\n amount DECIMAL(10,2),\\n transaction_type VARCHAR(20)\\n)\"],\"mssql\":[\"CREATE TABLE customer_transactions (\\n transaction_id INT,\\n customer_id INT,\\n transaction_date DATE,\\n amount DECIMAL(10,2),\\n transaction_type VARCHAR(20)\\n)\"],\"oraclesql\":[\"CREATE TABLE customer_transactions (\\n transaction_id NUMBER,\\n customer_id NUMBER,\\n transaction_date DATE,\\n amount NUMBER(10,2),\\n transaction_type VARCHAR2(20)\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_loyal_customers\",\"postgresql\":[\"CREATE TABLE IF NOT EXISTS customer_transactions (\\n transaction_id SERIAL PRIMARY KEY,\\n customer_id INT NOT NULL,\\n transaction_date DATE NOT NULL,\\n amount NUMERIC(10,2) NOT NULL,\\n transaction_type VARCHAR(20) NOT NULL\\n);\"],\"pythondata\":[\"customer_transactions = pd.DataFrame({\\n \\\"transaction_id\\\": pd.Series(dtype=\\\"int\\\"),\\n \\\"customer_id\\\": pd.Series(dtype=\\\"int\\\"),\\n \\\"transaction_date\\\": pd.Series(dtype=\\\"datetime64[ns]\\\"),\\n \\\"amount\\\": pd.Series(dtype=\\\"float\\\"),\\n \\\"transaction_type\\\": pd.Series(dtype=\\\"string\\\")\\n})\"],\"database_schema\":{\"customer_transactions\":{\"transaction_id\":\"INT\",\"customer_id\":\"INT\",\"transaction_date\":\"DATE\",\"amount\":\"DECIMAL(10, 2)\",\"transaction_type\":\"VARCHAR(20)\"}}}",
|
||
"judgerAvailable": true,
|
||
"judgeType": "large",
|
||
"mysqlSchemas": [
|
||
"CREATE TABLE if not exists customer_transactions (\n transaction_id INT,\n customer_id INT,\n transaction_date DATE,\n amount DECIMAL(10,2),\n transaction_type VARCHAR(20)\n)",
|
||
"Truncate table customer_transactions",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('1', '101', '2024-01-05', '150.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('2', '101', '2024-01-15', '200.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('3', '101', '2024-02-10', '180.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('4', '101', '2024-02-20', '250.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('5', '102', '2024-01-10', '100.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('6', '102', '2024-01-12', '120.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('7', '102', '2024-01-15', '80.0', 'refund')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('8', '102', '2024-01-18', '90.0', 'refund')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('9', '102', '2024-02-15', '130.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('10', '103', '2024-01-01', '500.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('11', '103', '2024-01-02', '450.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('12', '103', '2024-01-03', '400.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('13', '104', '2024-01-01', '200.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('14', '104', '2024-02-01', '250.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('15', '104', '2024-02-15', '300.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('16', '104', '2024-03-01', '350.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('17', '104', '2024-03-10', '280.0', 'purchase')",
|
||
"insert into customer_transactions (transaction_id, customer_id, transaction_date, amount, transaction_type) values ('18', '104', '2024-03-15', '100.0', 'refund')"
|
||
],
|
||
"enableRunCode": true,
|
||
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>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"
|
||
}
|
||
}
|
||
} |