某即时零售平台在晚高峰前,会给门店的骑手补位班次发送“待确认邀约”。同一个班次可能会在不同时间多次推送给不同骑手,也可能被同一骑手多次看到。运营团队希望统计指定日期范围内、每个门店每天“最终成功开班的补位班次”在发出邀约后的 2 小时内确认情况,并从中找出门店当日表现最好的补位班次。 表结构和字段说明如下。 表relief_shifts:补位班次主表。 字段shift_id:BIGINT,补位班次 ID,主键。 字段store_id:BIGINT,门店 ID。 字段shift_date:DATE,班次日期。 字段shift_period:VARCHAR(20),班次时段,可能值如lunch、afternoon、dinner、night。 字段required_rider_count:INT,该班次需要补位的骑手数量。 字段published_at:DATETIME,班次首次发出邀约的时间。 字段shift_status:VARCHAR(20),班次最终状态,可能值如filled、cancelled。 表shift_invite_responses:班次邀约反馈表。 字段response_id:BIGINT,反馈记录 ID,主键。 字段shift_id:BIGINT,所属补位班次 ID,对应relief_shifts.shift_id。 字段rider_id:BIGINT,骑手 ID。 字段invited_at:DATETIME,该次邀约发出时间。 字段responded_at:DATETIME,骑手反馈时间;若为NULL,表示未反馈。 字段response_status:VARCHAR(20),反馈结果,可能值如accepted、declined、ignored。 补充业务规则如下。 只统计shift_date在2025-08-15到2025-08-17之间,且shift_status = 'filled'的补位班次。 对于同一个shift_id + rider_id,可能存在多条邀约反馈记录。只保留 invited_at最早 的那条作为该骑手对该班次的“首轮邀约记录”。 “2 小时内确认成功”定义为: response_status = 'accepted' responded_at不为NULL 且responded_at accepted_in_2h_rider_count表示某班次中,满足“2 小时内确认成功”的不同骑手数。 acceptance_rate定义为: accepted_in_2h_rider_count required_rider_count 结果按 四舍五入保留 2 位小数 对每个store_id + shift_date,只保留acceptance_rate最高的那一个班次。 如果同一门店同一天有多个班次acceptance_rate相同,则依次取: accepted_in_2h_rider_count更大的 published_at更早的 shift_id更小的 3. 问题 请查询2025-08-15到2025-08-17期间,每个门店每天最终确认率最高的补位班次,返回以下字段: store_id:门店 ID shift_date:班次日期 shift_id:补位班次 ID shift_period:班次时段 required_rider_count:需要补位人数 accepted_in_2h_rider_count:2 小时内确认成功的骑手数 acceptance_rate:2 小时内确认率,四舍五入保留 2 位小数 结果按以下规则排序: shift_date升序 store_id升序 shift_id升序 4. 示例数据表 表relief_shifts shift_id store_id shift_date shift_period required_rider_count published_at shift_status 1101 301 2025-08-15 dinner 3 2025-08-15 16:00:00 filled 1102 301 2025-08-15 night 2 2025-08-15 18:30:00 filled 1103 302 2025-08-15 dinner 2 2025-08-15 15:30:00 cancelled 1104 301 2025-08-16 lunch 2 2025-08-16 09:00:00 filled 1105 302 2025-08-16 dinner 4 2025-08-16 16:10:00 filled 表shift_invite_responses response_id shift_id rider_id invited_at responded_at response_status 2101 1101 9001 2025-08-15 16:05:00 2025-08-15 16:40:00 accepted 2102 1101 9002 2025-08-15 16:10:00 2025-08-15 18:20:00 accepted 2103 1101 9003 2025-08-15 16:20:00 2025-08-15 18:30:01 accepted 2104 1102 9004 2025-08-15 18:35:00 2025-08-15 19:00:00 accepted 2105 1102 9005 2025-08-15 18:40:00 2025-08-15 20:30:00 accepted 2106 1104 9001 2025-08-16 09:05:00 2025-08-16 09:50:00 accepted 2107 1104 9001 2025-08-16 09:20:00 2025-08-16 09:30:00 accepted 2108 1104 9006 2025-08-16 09:10:00 2025-08-16 11:30:00 declined 2109 1105 9007 2025-08-16 16:15:00 2025-08-16 16:50:00 accepted 2110 1105 9008 2025-08-16 16:20:00 NULL ignored 2111 1105 9009 2025-08-16 16:25:00 2025-08-16 19:00:00 accepted 5. 示例数据查询结果表 store_id shift_date shift_id shift_period required_rider_count accepted_in_2h_rider_count acceptance_rate 301 2025-08-15 1102 night 2 2 1.00 301 2025-08-16 1104 lunch 2 1 0.50 302 2025-08-16 1105 dinner 4 1 0.25
示例1

输入

CREATE TABLE relief_shifts (
    shift_id BIGINT PRIMARY KEY,
    store_id BIGINT NOT NULL,
    shift_date DATE NOT NULL,
    shift_period VARCHAR(20) NOT NULL,
    required_rider_count INT NOT NULL,
    published_at DATETIME NOT NULL,
    shift_status VARCHAR(20) NOT NULL
);

CREATE TABLE shift_invite_responses (
    response_id BIGINT PRIMARY KEY,
    shift_id BIGINT NOT NULL,
    rider_id BIGINT NOT NULL,
    invited_at DATETIME NOT NULL,
    responded_at DATETIME NULL,
    response_status VARCHAR(20) NOT NULL
);

INSERT INTO relief_shifts (
    shift_id, store_id, shift_date, shift_period,
    required_rider_count, published_at, shift_status
) VALUES
(1101, 301, '2025-08-15', 'dinner', 3, '2025-08-15 16:00:00', 'filled'),
(1102, 301, '2025-08-15', 'night', 2, '2025-08-15 18:30:00', 'filled'),
(1103, 302, '2025-08-15', 'dinner', 2, '2025-08-15 15:30:00', 'cancelled'),
(1104, 301, '2025-08-16', 'lunch', 2, '2025-08-16 09:00:00', 'filled'),
(1105, 302, '2025-08-16', 'dinner', 4, '2025-08-16 16:10:00', 'filled');

INSERT INTO shift_invite_responses (
    response_id, shift_id, rider_id, invited_at, responded_at, response_status
) VALUES
(2101, 1101, 9001, '2025-08-15 16:05:00', '2025-08-15 16:40:00', 'accepted'),
(2102, 1101, 9002, '2025-08-15 16:10:00', '2025-08-15 18:20:00', 'accepted'),
(2103, 1101, 9003, '2025-08-15 16:20:00', '2025-08-15 18:30:01', 'accepted'),
(2104, 1102, 9004, '2025-08-15 18:35:00', '2025-08-15 19:00:00', 'accepted'),
(2105, 1102, 9005, '2025-08-15 18:40:00', '2025-08-15 20:30:00', 'accepted'),
(2106, 1104, 9001, '2025-08-16 09:05:00', '2025-08-16 09:50:00', 'accepted'),
(2107, 1104, 9001, '2025-08-16 09:20:00', '2025-08-16 09:30:00', 'accepted'),
(2108, 1104, 9006, '2025-08-16 09:10:00', '2025-08-16 11:30:00', 'declined'),
(2109, 1105, 9007, '2025-08-16 16:15:00', '2025-08-16 16:50:00', 'accepted'),
(2110, 1105, 9008, '2025-08-16 16:20:00', NULL, 'ignored'),
(2111, 1105, 9009, '2025-08-16 16:25:00', '2025-08-16 19:00:00', 'accepted');

输出

store_id|shift_date|shift_id|shift_period|required_rider_count|accepted_in_2h_rider_count|acceptance_rate
301|2025-08-15|1102|night|2|2|1.00
301|2025-08-16|1104|lunch|2|1|0.50
302|2025-08-16|1105|dinner|4|1|0.25
加载中...