{ "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| 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\n3
orders.60%
of their orders are during peak hours (11:00
-14:00
or 18:00
-21:00
).4.0,
round it to 2
decimal places.50%
of their orders.Return the result table ordered by average_rating
in descending order, then by customer_id
in descending order.
The result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nrestaurant_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\nThe results table is ordered by average_rating DESC, then customer_id DESC.
\n表:restaurant_orders
\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\n3
笔订单。60%
的订单在 高峰时间 中(11:00
-14:00
或 18:00
-21:00
)。4.0
,四舍五入到小数点后 2
位。50%
的订单。返回结果表按 average_rating
降序 排序,然后按 customer_id
降序 排序。
结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nrestaurant_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结果表按 average_rating 降序排序,然后按 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\":{\"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"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"