{ "data": { "question": { "questionId": "4025", "questionFrontendId": "3657", "categoryTitle": "Database", "boundTopicId": 3761718, "title": "Find Loyal Customers", "titleSlug": "find-loyal-customers", "content": "

Table: customer_transactions

\n\n
\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\n\n\n

Return the result table ordered by customer_id in ascending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

customer_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\n\n\n

The result table is ordered by customer_id in ascending order.

\n
\n", "translatedTitle": "寻找忠实客户", "translatedContent": "

表:customer_transactions

\n\n
\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\n\n\n

返回结果表以 customer_id 升序 排序。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

customer_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\n\n

结果表以 customer_id 升序排序。

\n
\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\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

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" } } }