# Write your MySQL query statement below
1174. 即时食物配送 II
配送表: Delivery
+-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+ delivery_id 是表的主键。 该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。
如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。
「首次订单」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。
写一条 SQL 查询语句获取即时订单在所有用户的首次订单中的比例。保留两位小数。
查询结果如下所示:
Delivery 表: +-------------+-------------+------------+-----------------------------+ | delivery_id | customer_id | order_date | customer_pref_delivery_date | +-------------+-------------+------------+-----------------------------+ | 1 | 1 | 2019-08-01 | 2019-08-02 | | 2 | 2 | 2019-08-02 | 2019-08-02 | | 3 | 1 | 2019-08-11 | 2019-08-12 | | 4 | 3 | 2019-08-24 | 2019-08-24 | | 5 | 3 | 2019-08-21 | 2019-08-22 | | 6 | 2 | 2019-08-11 | 2019-08-13 | | 7 | 4 | 2019-08-09 | 2019-08-09 | +-------------+-------------+------------+-----------------------------+ Result 表: +----------------------+ | immediate_percentage | +----------------------+ | 50.00 | +----------------------+ 1 号顾客的 1 号订单是首次订单,并且是计划订单。 2 号顾客的 2 号订单是首次订单,并且是即时订单。 3 号顾客的 5 号订单是首次订单,并且是计划订单。 4 号顾客的 7 号订单是首次订单,并且是即时订单。 因此,一半顾客的首次订单是即时的。
原站题解
pythondata 解法, 执行用时: 400 ms, 内存消耗: 67.6 MB, 提交时间: 2024-05-27 12:56:46
import pandas as pd def immediate_food_delivery2(delivery: pd.DataFrame) -> pd.DataFrame: delivery.sort_values(by='order_date',inplace=True) #对日期进行升序排序 delivery.drop_duplicates(subset='customer_id',keep='first',inplace=True) #去除顾客号相同的并保留第一个记录 equal=delivery[delivery['order_date']==delivery['customer_pref_delivery_date']] #找到即时订单 bili=round(len(equal)/len(delivery)*100,2) return pd.DataFrame({'immediate_percentage':[bili]}) def immediate_food_delivery(delivery: pd.DataFrame) -> pd.DataFrame: # 使用groupby和min找到每个客户的首次订单日期 first_order_dates = delivery.groupby('customer_id')['order_date'].min().reset_index() # 合并首次订单日期回到原始数据集,以便筛选出首次订单的记录 first_orders = pd.merge(delivery, first_order_dates, on=['customer_id', 'order_date'], how='inner') # 计算订单日期与客户首选配送日期相同的情况的百分比 immediate_delivery_percentage = round(100 * (first_orders['order_date'] == first_orders['customer_pref_delivery_date']).sum() / len(first_orders), 2) # 创建包含结果的DataFrame result_df = pd.DataFrame({'immediate_percentage': [immediate_delivery_percentage]}) return result_df
mysql 解法, 执行用时: 416 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:10:25
# Write your MySQL query statement below SELECT ROUND(COUNT(a.customer_id)/COUNT(b.customer_id)*100,2) AS 'immediate_percentage' FROM ( SELECT customer_id,MIN(order_date) AS 'fir_ins_o' FROM Delivery WHERE order_date = customer_pref_delivery_date GROUP BY customer_id )a RIGHT JOIN ( SELECT customer_id,MIN(order_date) AS 'fir_o' FROM Delivery GROUP BY customer_id )b ON a.customer_id = b.customer_id AND a.fir_ins_o = b.fir_o
mysql 解法, 执行用时: 396 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:09:52
# Write your MySQL query statement below select round ( sum(order_date = customer_pref_delivery_date) * 100 / count(*), 2 ) as immediate_percentage from Delivery where (customer_id, order_date) in ( select customer_id, min(order_date) from delivery group by customer_id );