某新能源运营商管理大量超充站。充电站发生故障告警后,调度系统会先创建一条首发派单;如果现场工程师发现问题需要继续拆分、升级或转交,就会基于上一条派单再生成下游派单,形成一条“故障处置链”。 运维团队希望统计指定时间范围内,某运营商发起的每一条故障处置链的复杂度和处理工时。 本题使用 MySQL 8.0 语法,核心考点是 WITH RECURSIVE ... AS (...)。 表结构和字段说明如下。 表charging_stations:充电站主数据表。 字段station_id:BIGINT,充电站 ID,主键。 字段operator_name:VARCHAR(100),运营商名称。 字段station_name:VARCHAR(200),充电站名称。 字段city:VARCHAR(50),所在城市。 字段launched_at:DATETIME,充电站上线时间。 表fault_dispatch_tasks:故障派单表。 字段task_id:BIGINT,派单 ID,主键。 字段station_id:BIGINT,所属充电站 ID,对应charging_stations.station_id。 字段engineer_name:VARCHAR(100),处理工程师姓名。 字段parent_task_id:BIGINT,上游派单 ID;若为NULL,表示该记录是首发派单。 字段fault_type:VARCHAR(30),故障类型,例如power、network、module、cooling。 字段task_status:VARCHAR(20),派单状态,例如resolved、escalated、pending。 字段dispatched_at:DATETIME,派单创建时间。 字段handling_minutes:INT,该派单实际处理耗时,单位分钟。 补充业务规则如下。 一条故障处置链从一条parent_task_id IS NULL的首发派单开始。 某条派单的下游派单,是指fault_dispatch_tasks.parent_task_id = 当前 task_id的记录。 需要沿着派单关系递归向下展开,直到没有新的下游派单为止。 本题只统计operator_name = '极曜能源'的充电站。 首发派单必须发生在2025-11-05 00:00:00到2025-11-09 23:59:59之间。 只要某条派单属于这些首发派单的递归下游,即使它自己的dispatched_at超出上述时间范围,也应计入对应故障处置链。 若某个节点在本处置链内没有任何子节点,则它是叶子节点。 如果某条首发派单没有任何下游派单,则该首发派单本身也算 1 个叶子节点。 resolved_leaf_count只统计“叶子节点中task_status = 'resolved'的数量”。 distinct_fault_type_count按整条处置链内不同的fault_type去重统计。 3. 问题 请查询operator_name = '极曜能源'且首发派单发生在2025-11-05 00:00:00到2025-11-09 23:59:59之间的每一条首发故障处置链,返回以下字段: root_task_id:首发派单 ID station_name:充电站名称 total_task_count:该故障处置链的派单总数,包含首发派单自身 max_depth:该故障处置链的最大层级深度,首发派单深度记为0 resolved_leaf_count:该故障处置链中状态为resolved的叶子节点数量 distinct_fault_type_count:该故障处置链中不同故障类型的数量 total_handling_hours:该故障处置链内所有节点handling_minutes之和换算成小时,并四舍五入保留 2 位小数 结果按以下规则排序: resolved_leaf_count降序 max_depth降序 total_task_count降序 root_task_id升序 4. 示例数据表 表charging_stations station_id operator_name station_name city launched_at 901 极曜能源 浦东枢纽超充站 上海 2025-01-10 09:00:00 902 极曜能源 南山科技园充电站 深圳 2025-02-18 10:00:00 903 星泊电能 天府软件园充电站 成都 2025-03-12 11:00:00 表fault_dispatch_tasks task_id station_id engineer_name parent_task_id fault_type task_status dispatched_at handling_minutes 12001 901 张屿 NULL power escalated 2025-11-05 08:00:00 30 12002 901 刘谨 12001 module escalated 2025-11-05 09:00:00 25 12003 901 陈舟 12001 network resolved 2025-11-05 09:20:00 20 12004 901 孙岚 12002 module resolved 2025-11-05 10:10:00 35 12005 902 王策 NULL cooling escalated 2025-11-06 14:00:00 40 12006 902 周湛 12005 cooling resolved 2025-11-06 16:00:00 50 12007 903 何远 NULL power resolved 2025-11-07 11:00:00 45 5. 示例数据查询结果表 root_task_id station_name total_task_count max_depth resolved_leaf_count distinct_fault_type_count total_handling_hours 12001 浦东枢纽超充站 4 2 2 3 1.83 12005 南山科技园充电站 2 1 1 1 1.50
示例1
输入
CREATE TABLE charging_stations (
station_id BIGINT PRIMARY KEY,
operator_name VARCHAR(100) NOT NULL,
station_name VARCHAR(200) NOT NULL,
city VARCHAR(50) NOT NULL,
launched_at DATETIME NOT NULL
);
CREATE TABLE fault_dispatch_tasks (
task_id BIGINT PRIMARY KEY,
station_id BIGINT NOT NULL,
engineer_name VARCHAR(100) NOT NULL,
parent_task_id BIGINT NULL,
fault_type VARCHAR(30) NOT NULL,
task_status VARCHAR(20) NOT NULL,
dispatched_at DATETIME NOT NULL,
handling_minutes INT NOT NULL
);
INSERT INTO charging_stations (station_id, operator_name, station_name, city, launched_at) VALUES
(901, '极曜能源', '浦东枢纽超充站', '上海', '2025-01-10 09:00:00'),
(902, '极曜能源', '南山科技园充电站', '深圳', '2025-02-18 10:00:00'),
(903, '星泊电能', '天府软件园充电站', '成都', '2025-03-12 11:00:00');
INSERT INTO fault_dispatch_tasks (
task_id, station_id, engineer_name, parent_task_id,
fault_type, task_status, dispatched_at, handling_minutes
) VALUES
(12001, 901, '张屿', NULL, 'power', 'escalated', '2025-11-05 08:00:00', 30),
(12002, 901, '刘谨', 12001, 'module', 'escalated', '2025-11-05 09:00:00', 25),
(12003, 901, '陈舟', 12001, 'network', 'resolved', '2025-11-05 09:20:00', 20),
(12004, 901, '孙岚', 12002, 'module', 'resolved', '2025-11-05 10:10:00', 35),
(12005, 902, '王策', NULL, 'cooling', 'escalated', '2025-11-06 14:00:00', 40),
(12006, 902, '周湛', 12005, 'cooling', 'resolved', '2025-11-06 16:00:00', 50),
(12007, 903, '何远', NULL, 'power', 'resolved', '2025-11-07 11:00:00', 45);
输出
root_task_id|station_name|total_task_count|max_depth|resolved_leaf_count|distinct_fault_type_count|total_handling_hours
12001|浦东枢纽超充站|4|2|2|3|1.83
12005|南山科技园充电站|2|1|1|1|1.50
加载中...