【背景】:饿了么需要分析不同配送员在不同天气条件下的配送效率和用户投诉情况,以便优化配送服务和管理配送团队。 【原始表】: delivery_staff(配送员)表: staff_id (配送员 ID): 配送员的唯一标识符,INT staff_name (配送员姓名): 配送员的姓名,VARCHAR(50) average_speed (平均配送速度): 配送员的平均配送速度,FLOAT weather_conditions (天气条件)表: weather_id (天气 ID): 天气的唯一标识符,INT weather_type (天气类型): 天气的类型,VARCHAR(20)(晴天、雨天、雪天等) delivery_records (配送记录)表: record_id (记录 ID): 配送记录的唯一标识符,INT staff_id (配送员 ID): 关联配送员表的配送员 ID,INT weather_id (天气 ID): 关联天气条件表的天气 ID,INT delivery_time (配送时间): 配送完成所需的时间,INT is_complaint(是否被投诉): 是否被投诉,INT(1代表被投诉,0代表未被投诉) 【要求】:根据上述表格,查询出每种天气类型下,平均配送速度大于 20 且投诉率(投诉数量 配送订单总数量)低于50%的所有配送员的每单平均配送时间,配送的总次数。查询结果按照天气类型升序排列。要求查询出来的表格的字段如下: weather_type: 天气类型。 average_delivery_time: 平均配送时间。(round保留2位小数) delivery_count: 配送总次数。 【示例】 delivery_staff(配送员)表: weather_conditions (天气条件)表: delivery_records (配送记录)表: 【按要求查询出来的表】 【解释】 上述示例中天气类型涉及2种,分别是weather_id (天气 ID) 1和2,其中weather_id (天气 ID) 1相关的配送记录有2条,分别是record_id 1和3,record_id 1和3的配送员都是staff_id (配送员 ID) 1,配送速度是25超过了20,且该配送员的投诉率 = 0 2 = 0;所以该配送员符合要求,该配送员的每单平均配送时间是(30+20)2 = 25;配送总次数是2;
示例1
输入
DROP TABLE IF EXISTS delivery_staff ;
DROP TABLE IF EXISTS weather_conditions;
DROP TABLE IF EXISTS delivery_records;
-- 创建表
CREATE TABLE delivery_staff (
staff_id INT PRIMARY KEY,
staff_name VARCHAR(50),
average_speed FLOAT
);
CREATE TABLE weather_conditions (
weather_id INT PRIMARY KEY,
weather_type VARCHAR(20)
);
CREATE TABLE delivery_records (
record_id INT PRIMARY KEY,
staff_id INT,
weather_id INT,
delivery_time INT,
is_complaint INT
);
-- 插入数据
INSERT INTO delivery_staff (staff_id, staff_name, average_speed)
VALUES (1, '张三', 25.0),
(2, '李四', 18.0),
(3, '王五', 21.0);
INSERT INTO weather_conditions(weather_id, weather_type)
VALUES (1, '晴天'),
(2, '雨天');
INSERT INTO delivery_records(record_id, staff_id, weather_id, delivery_time, is_complaint)
VALUES (1, 1, 1, 30, 0),
(2, 2, 2, 40, 1),
(3, 1, 1, 20, 0),
(4, 2, 2, 40, 1),
(5, 3, 2, 30, 1);
select * from delivery_staff;
select * from weather_conditions;
select * from delivery_records;
输出
weather_type|average_delivery_time|delivery_count
晴天|25.00|2
加载中...