{ "data": { "question": { "questionId": "4091", "questionFrontendId": "3705", "categoryTitle": "Database", "boundTopicId": 3798940, "title": "Find Golden Hour Customers", "titleSlug": "find-golden-hour-customers", "content": "

Table: restaurant_orders

\n\n
\n+------------------+----------+\n| Column Name      | Type     | \n+------------------+----------+\n| order_id         | int      |\n| customer_id      | int      |\n| order_timestamp  | datetime |\n| order_amount     | decimal  |\n| payment_method   | varchar  |\n| order_rating     | int      |\n+------------------+----------+\norder_id is the unique identifier for this table.\npayment_method can be cash, card, or app.\norder_rating is between 1 and 5, where 5 is the best (NULL if not rated).\norder_timestamp contains both date and time information.\n
\n\n

Write a solution to find golden hour customers - customers who consistently order during peak hours and provide high satisfaction. A customer is a golden hour customer if they meet ALL the following criteria:

\n\n\n\n

Return the result table ordered by average_rating in descending order, then by customer_id​​​​​​​ in descending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

restaurant_orders table:

\n\n
\n+----------+-------------+---------------------+--------------+----------------+--------------+\n| order_id | customer_id | order_timestamp     | order_amount | payment_method | order_rating |\n+----------+-------------+---------------------+--------------+----------------+--------------+\n| 1        | 101         | 2024-03-01 12:30:00 | 25.50        | card           | 5            |\n| 2        | 101         | 2024-03-02 19:15:00 | 32.00        | app            | 4            |\n| 3        | 101         | 2024-03-03 13:45:00 | 28.75        | card           | 5            |\n| 4        | 101         | 2024-03-04 20:30:00 | 41.00        | app            | NULL         |\n| 5        | 102         | 2024-03-01 11:30:00 | 18.50        | cash           | 4            |\n| 6        | 102         | 2024-03-02 12:00:00 | 22.00        | card           | 3            |\n| 7        | 102         | 2024-03-03 15:30:00 | 19.75        | cash           | NULL         |\n| 8        | 103         | 2024-03-01 19:00:00 | 55.00        | app            | 5            |\n| 9        | 103         | 2024-03-02 20:45:00 | 48.50        | app            | 4            |\n| 10       | 103         | 2024-03-03 18:30:00 | 62.00        | card           | 5            |\n| 11       | 104         | 2024-03-01 10:00:00 | 15.00        | cash           | 3            |\n| 12       | 104         | 2024-03-02 09:30:00 | 18.00        | cash           | 2            |\n| 13       | 104         | 2024-03-03 16:00:00 | 20.00        | card           | 3            |\n| 14       | 105         | 2024-03-01 12:15:00 | 30.00        | app            | 4            |\n| 15       | 105         | 2024-03-02 13:00:00 | 35.50        | app            | 5            |\n| 16       | 105         | 2024-03-03 11:45:00 | 28.00        | card           | 4            |\n+----------+-------------+---------------------+--------------+----------------+--------------+\n
\n\n

Output:

\n\n
\n+-------------+--------------+----------------------+----------------+\n| customer_id | total_orders | peak_hour_percentage | average_rating |\n+-------------+--------------+----------------------+----------------+\n| 103         | 3            | 100                  | 4.67           |\n| 101         | 4            | 75                   | 4.67           |\n| 105         | 3            | 100                  | 4.33           |\n+-------------+--------------+----------------------+----------------+\n
\n\n

Explanation:

\n\n\n\n

The results table is ordered by average_rating DESC, then customer_id DESC.

\n
\n", "translatedTitle": "寻找黄金时段客户", "translatedContent": "

表:restaurant_orders

\n\n
\n+------------------+----------+\n| Column Name      | Type     | \n+------------------+----------+\n| order_id         | int      |\n| customer_id      | int      |\n| order_timestamp  | datetime |\n| order_amount     | decimal  |\n| payment_method   | varchar  |\n| order_rating     | int      |\n+------------------+----------+\norder_id 是这张表的唯一主键。\npayment_method 可以是 cash,card 或 app。\norder_rating 在 1 到 5 之间,其中 5 是最佳(如果没有评分则是 NULL)。\norder_timestamp 同时包含日期和时间信息。\n
\n\n

编写一个解决方案来寻找 黄金时间客户 - 高峰时段持续订购且满意度高的客户。客户若满足以下所有条件,则被视为 黄金时段客户

\n\n\n\n

返回结果表按 average_rating 降序 排序,然后按 customer_id 降序 排序。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

restaurant_orders 表:

\n\n
\n+----------+-------------+---------------------+--------------+----------------+--------------+\n| order_id | customer_id | order_timestamp     | order_amount | payment_method | order_rating |\n+----------+-------------+---------------------+--------------+----------------+--------------+\n| 1        | 101         | 2024-03-01 12:30:00 | 25.50        | card           | 5            |\n| 2        | 101         | 2024-03-02 19:15:00 | 32.00        | app            | 4            |\n| 3        | 101         | 2024-03-03 13:45:00 | 28.75        | card           | 5            |\n| 4        | 101         | 2024-03-04 20:30:00 | 41.00        | app            | NULL         |\n| 5        | 102         | 2024-03-01 11:30:00 | 18.50        | cash           | 4            |\n| 6        | 102         | 2024-03-02 12:00:00 | 22.00        | card           | 3            |\n| 7        | 102         | 2024-03-03 15:30:00 | 19.75        | cash           | NULL         |\n| 8        | 103         | 2024-03-01 19:00:00 | 55.00        | app            | 5            |\n| 9        | 103         | 2024-03-02 20:45:00 | 48.50        | app            | 4            |\n| 10       | 103         | 2024-03-03 18:30:00 | 62.00        | card           | 5            |\n| 11       | 104         | 2024-03-01 10:00:00 | 15.00        | cash           | 3            |\n| 12       | 104         | 2024-03-02 09:30:00 | 18.00        | cash           | 2            |\n| 13       | 104         | 2024-03-03 16:00:00 | 20.00        | card           | 3            |\n| 14       | 105         | 2024-03-01 12:15:00 | 30.00        | app            | 4            |\n| 15       | 105         | 2024-03-02 13:00:00 | 35.50        | app            | 5            |\n| 16       | 105         | 2024-03-03 11:45:00 | 28.00        | card           | 4            |\n+----------+-------------+---------------------+--------------+----------------+--------------+\n
\n\n

输出:

\n\n
\n+-------------+--------------+----------------------+----------------+\n| customer_id | total_orders | peak_hour_percentage | average_rating |\n+-------------+--------------+----------------------+----------------+\n| 103         | 3            | 100                  | 4.67           |\n| 101         | 4            | 75                   | 4.67           |\n| 105         | 3            | 100                  | 4.33           |\n+-------------+--------------+----------------------+----------------+\n
\n\n

解释:

\n\n\n\n

结果表按 average_rating 降序排序,然后按 customer_id 降序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python3\": false, \"python\": false, \"javascript\": false, \"typescript\": false, \"csharp\": false, \"c\": false, \"golang\": false, \"kotlin\": false, \"swift\": false, \"rust\": false, \"ruby\": false, \"php\": false, \"dart\": false, \"scala\": false, \"elixir\": false, \"erlang\": false, \"racket\": false, \"cangjie\": false, \"bash\": false, \"html\": false, \"pythonml\": false, \"react\": false, \"vanillajs\": false, \"mysql\": false, \"mssql\": false, \"postgresql\": false, \"oraclesql\": false, \"pythondata\": 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": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL 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_golden_hour_customers(restaurant_orders: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"196\", \"totalSubmission\": \"350\", \"totalAcceptedRaw\": 196, \"totalSubmissionRaw\": 350, \"acRate\": \"56.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"restaurant_orders\":[\"order_id\",\"customer_id\",\"order_timestamp\",\"order_amount\",\"payment_method\",\"order_rating\"]},\"rows\":{\"restaurant_orders\":[[1,101,\"2024-03-01 12:30:00\",25.50,\"card\",5],[2,101,\"2024-03-02 19:15:00\",32.00,\"app\",4],[3,101,\"2024-03-03 13:45:00\",28.75,\"card\",5],[4,101,\"2024-03-04 20:30:00\",41.00,\"app\",null],[5,102,\"2024-03-01 11:30:00\",18.50,\"cash\",4],[6,102,\"2024-03-02 12:00:00\",22.00,\"card\",3],[7,102,\"2024-03-03 15:30:00\",19.75,\"cash\",null],[8,103,\"2024-03-01 19:00:00\",55.00,\"app\",5],[9,103,\"2024-03-02 20:45:00\",48.50,\"app\",4],[10,103,\"2024-03-03 18:30:00\",62.00,\"card\",5],[11,104,\"2024-03-01 10:00:00\",15.00,\"cash\",3],[12,104,\"2024-03-02 09:30:00\",18.00,\"cash\",2],[13,104,\"2024-03-03 16:00:00\",20.00,\"card\",3],[14,105,\"2024-03-01 12:15:00\",30.00,\"app\",4],[15,105,\"2024-03-02 13:00:00\",35.50,\"app\",5],[16,105,\"2024-03-03 11:45:00\",28.00,\"card\",4]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE restaurant_orders (\\n order_id INT,\\n customer_id INT,\\n order_timestamp DATETIME,\\n order_amount DECIMAL(10,2),\\n payment_method VARCHAR(10),\\n order_rating INT\\n)\"],\"mssql\":[\"CREATE TABLE restaurant_orders (\\n order_id INT,\\n customer_id INT,\\n order_timestamp DATETIME,\\n order_amount DECIMAL(10,2),\\n payment_method VARCHAR(10),\\n order_rating INT\\n)\"],\"oraclesql\":[\"CREATE TABLE restaurant_orders (\\n order_id NUMBER,\\n customer_id NUMBER,\\n order_timestamp DATE,\\n order_amount NUMBER(10,2),\\n payment_method VARCHAR2(10),\\n order_rating NUMBER\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD HH24:MI:SS'\"],\"database\":true,\"name\":\"find_golden_hour_customers\",\"pythondata\":[\"restaurant_orders = pd.DataFrame({\\n \\\"order_id\\\": pd.Series(dtype=\\\"int\\\"),\\n \\\"customer_id\\\": pd.Series(dtype=\\\"int\\\"),\\n \\\"order_timestamp\\\": pd.Series(dtype=\\\"datetime64[ns]\\\"),\\n \\\"order_amount\\\": pd.Series(dtype=\\\"float\\\"),\\n \\\"payment_method\\\": pd.Series(dtype=\\\"string\\\"),\\n \\\"order_rating\\\": pd.Series(dtype=\\\"Int64\\\") # nullable integer for ratings that can be NULL\\n})\"],\"postgresql\":[\"CREATE TABLE restaurant_orders (\\n order_id SERIAL PRIMARY KEY,\\n customer_id INT,\\n order_timestamp TIMESTAMP,\\n order_amount NUMERIC(10,2),\\n payment_method VARCHAR(10),\\n order_rating INT\\n);\\n\"],\"database_schema\":{\"restaurant_orders\":{\"order_id\":\"INT\",\"customer_id\":\"INT\",\"order_timestamp\":\"DATETIME\",\"order_amount\":\"DECIMAL(10, 2)\",\"payment_method\":\"VARCHAR(10)\",\"order_rating\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE restaurant_orders (\n order_id INT,\n customer_id INT,\n order_timestamp DATETIME,\n order_amount DECIMAL(10,2),\n payment_method VARCHAR(10),\n order_rating INT\n)", "Truncate table restaurant_orders", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('1', '101', '2024-03-01 12:30:00', '25.5', 'card', '5')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('2', '101', '2024-03-02 19:15:00', '32.0', 'app', '4')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('3', '101', '2024-03-03 13:45:00', '28.75', 'card', '5')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('4', '101', '2024-03-04 20:30:00', '41.0', 'app', NULL)", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('5', '102', '2024-03-01 11:30:00', '18.5', 'cash', '4')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('6', '102', '2024-03-02 12:00:00', '22.0', 'card', '3')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('7', '102', '2024-03-03 15:30:00', '19.75', 'cash', NULL)", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('8', '103', '2024-03-01 19:00:00', '55.0', 'app', '5')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('9', '103', '2024-03-02 20:45:00', '48.5', 'app', '4')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('10', '103', '2024-03-03 18:30:00', '62.0', 'card', '5')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('11', '104', '2024-03-01 10:00:00', '15.0', 'cash', '3')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('12', '104', '2024-03-02 09:30:00', '18.0', 'cash', '2')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('13', '104', '2024-03-03 16:00:00', '20.0', 'card', '3')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('14', '105', '2024-03-01 12:15:00', '30.0', 'app', '4')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('15', '105', '2024-03-02 13:00:00', '35.5', 'app', '5')", "insert into restaurant_orders (order_id, customer_id, order_timestamp, order_amount, payment_method, order_rating) values ('16', '105', '2024-03-03 11:45:00', '28.0', 'card', '4')" ], "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\":{\"restaurant_orders\":[\"order_id\",\"customer_id\",\"order_timestamp\",\"order_amount\",\"payment_method\",\"order_rating\"]},\"rows\":{\"restaurant_orders\":[[1,101,\"2024-03-01 12:30:00\",25.50,\"card\",5],[2,101,\"2024-03-02 19:15:00\",32.00,\"app\",4],[3,101,\"2024-03-03 13:45:00\",28.75,\"card\",5],[4,101,\"2024-03-04 20:30:00\",41.00,\"app\",null],[5,102,\"2024-03-01 11:30:00\",18.50,\"cash\",4],[6,102,\"2024-03-02 12:00:00\",22.00,\"card\",3],[7,102,\"2024-03-03 15:30:00\",19.75,\"cash\",null],[8,103,\"2024-03-01 19:00:00\",55.00,\"app\",5],[9,103,\"2024-03-02 20:45:00\",48.50,\"app\",4],[10,103,\"2024-03-03 18:30:00\",62.00,\"card\",5],[11,104,\"2024-03-01 10:00:00\",15.00,\"cash\",3],[12,104,\"2024-03-02 09:30:00\",18.00,\"cash\",2],[13,104,\"2024-03-03 16:00:00\",20.00,\"card\",3],[14,105,\"2024-03-01 12:15:00\",30.00,\"app\",4],[15,105,\"2024-03-02 13:00:00\",35.50,\"app\",5],[16,105,\"2024-03-03 11:45:00\",28.00,\"card\",4]]}}", "__typename": "QuestionNode" } } }