列表

详情


1173. 即时食物配送 I

配送表: Delivery

+-----------------------------+---------+
| Column Name                 | Type    |
+-----------------------------+---------+
| delivery_id                 | int     |
| customer_id                 | int     |
| order_date                  | date    |
| customer_pref_delivery_date | date    |
+-----------------------------+---------+
在 SQL 中,delivery_id 是表的主键。
该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。

 

如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。

查询即时订单所占的百分比, 保留两位小数。

查询结果如下所示。

 

示例 1:

输入:
Delivery 表:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1           | 1           | 2019-08-01 | 2019-08-02                  |
| 2           | 5           | 2019-08-02 | 2019-08-02                  |
| 3           | 1           | 2019-08-11 | 2019-08-11                  |
| 4           | 3           | 2019-08-24 | 2019-08-26                  |
| 5           | 4           | 2019-08-21 | 2019-08-22                  |
| 6           | 2           | 2019-08-11 | 2019-08-13                  |
+-------------+-------------+------------+-----------------------------+
输出:
+----------------------+
| immediate_percentage |
+----------------------+
| 33.33                |
+----------------------+
解释:2 和 3 号订单为即时订单,其他的为计划订单。

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Write your MySQL query statement below

pythondata 解法, 执行用时: 272 ms, 内存消耗: 59.4 MB, 提交时间: 2023-08-09 15:00:21

import pandas as pd

def food_delivery(delivery: pd.DataFrame) -> pd.DataFrame:
    # 计算即时订单所占的百分比
    immediate_percentage = round((delivery['order_date'] == delivery['customer_pref_delivery_date']).mean() * 100, 2)
    
    return pd.DataFrame({'immediate_percentage': [immediate_percentage]})

mysql 解法, 执行用时: 446 ms, 内存消耗: 0 B, 提交时间: 2023-07-31 09:45:32

# Write your MySQL query statement below
# 0x03 sum
select round (
    sum(order_date = customer_pref_delivery_date) /
    count(*) * 100,
    2
) as immediate_percentage
from Delivery;

mysql 解法, 执行用时: 340 ms, 内存消耗: 0 B, 提交时间: 2023-07-31 09:45:00

# Write your MySQL query statement below
# 0x02 sum 和 case when
select round (
    sum(case when order_date = customer_pref_delivery_date then 1 else 0 end) /
    count(*) * 100,
    2
) as immediate_percentage
from Delivery;

mysql 解法, 执行用时: 526 ms, 内存消耗: 0 B, 提交时间: 2023-07-31 09:44:44

# Write your MySQL query statement below
# 0x01 直接计算
select round (
    (select count(*) from Delivery where order_date = customer_pref_delivery_date) / 
    (select count(*) from Delivery) * 100,
    2
) as immediate_percentage;

上一题