gpt4 book ai didi

sql - 根据特定标准对记录进行分组并查找最大值

转载 作者:行者123 更新时间:2023-12-01 19:23:19 24 4
gpt4 key购买 nike

我有一个veh_speed包含字段 vid 的表, date_time , speed , status 。我的目标是获取速度大于 30 的车辆的持续时间( start_date_timeend_date_time )。目前我正在使用 PL/SQL 生成报告。 。是否有可能与SQL有关。如果能够获得范围内的 max_speed,那就太好了。

我的表格如下:

VID  START_DATE_TIME        SPEED  STATUS
--- ------------------- ----- ------
1 15/01/2014 10:00:05 0 N
1 15/01/2014 10:00:10 10 Y
1 15/01/2014 10:00:15 30 Y
1 15/01/2014 10:00:20 35 Y
1 15/01/2014 10:00:25 45 Y
1 15/01/2014 10:00:27 10 Y
1 15/01/2014 10:00:29 0 Y
1 15/01/2014 10:00:30 20 Y
1 15/01/2014 10:00:35 32 Y
1 15/01/2014 10:00:40 33 Y
1 15/01/2014 10:00:45 35 Y
1 15/01/2014 10:00:50 38 Y
1 15/01/2014 10:00:55 10 Y

我想得到以下输出:

VID   START_DATE_TIME          END_DATE_TIME          MAX_SPEED
--- --------------- ------------- ---------
1 15/01/2014 10:00:15 15/01/2014 10:00:25 45
1 15/01/2014 10:00:35 15/01/2014 10:00:50 38

这是表创建脚本:

CREATE TABLE veh_speed(vid NUMBER(3), 
date_time DATE,
speed NUMBER(3),
status CHAR(1));

INSERT ALL
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:05', 'dd/mm/yyyy hh24:mi:ss'), 0, 'N')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:10', 'dd/mm/yyyy hh24:mi:ss'), 10, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:15', 'dd/mm/yyyy hh24:mi:ss'), 30, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:20', 'dd/mm/yyyy hh24:mi:ss'), 35, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:25', 'dd/mm/yyyy hh24:mi:ss'), 45, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:27', 'dd/mm/yyyy hh24:mi:ss'), 10, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:29', 'dd/mm/yyyy hh24:mi:ss'), 0, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:30', 'dd/mm/yyyy hh24:mi:ss'), 20, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:35', 'dd/mm/yyyy hh24:mi:ss'), 32, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:40', 'dd/mm/yyyy hh24:mi:ss'), 33, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:45', 'dd/mm/yyyy hh24:mi:ss'), 35, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:50', 'dd/mm/yyyy hh24:mi:ss'), 38, 'Y')
INTO veh_speed VALUES(1, to_date('15/01/2014 10:00:55', 'dd/mm/yyyy hh24:mi:ss'), 10, 'Y')
SELECT * FROM dual;

我希望我的问题说清楚了。

提前致谢。

最佳答案

您可以使用分析函数将记录分组为速度为 30 或更高的 block :

select vid, date_time, speed, status,
case when speed >= 30 then 30 else 0 end as speed_limit,
row_number() over (partition by vid order by date_time)
- row_number() over (
partition by vid, case when speed >= 30 then 30 else 0 end
order by date_time) as chain
from veh_speed;

VID DATE_TIME SPEED STATUS SPEED_LIMIT CHAIN
---------- ------------------- ---------- ------ ----------- ----------
1 15/01/2014 10:00:05 0 N 0 0
1 15/01/2014 10:00:10 10 Y 0 0
1 15/01/2014 10:00:15 30 Y 30 2
1 15/01/2014 10:00:20 35 Y 30 2
1 15/01/2014 10:00:25 45 Y 30 2
1 15/01/2014 10:00:27 10 Y 0 3
1 15/01/2014 10:00:29 0 Y 0 3
1 15/01/2014 10:00:30 20 Y 0 3
1 15/01/2014 10:00:35 32 Y 30 5
1 15/01/2014 10:00:40 33 Y 30 5
1 15/01/2014 10:00:45 35 Y 30 5
1 15/01/2014 10:00:50 38 Y 30 5
1 15/01/2014 10:00:55 10 Y 0 7

我不能将使用两个 row_number() 调用来生成记录链的技巧归功于我,不幸的是,我在某个地方找到了它(可能是 here )。 chain 的实际值并不重要,只是它们在每个 vid 中都是唯一的,并且对于符合您的条件的连续记录 block 中的所有记录而言都是相同的。

您只对“速度限制”为 30 的相关记录链感兴趣(这很容易是 Y/N 标志或其他),因此您可以使用它并过滤掉那些链条速度小于30;然后使用普通的聚合函数来得到你想要的:

select vid,
min(date_time) as start_date_time,
max(date_time) as end_date_time,
max(speed) as max_speed
from (
select vid, date_time, speed, status,
case when speed >= 30 then 30 else 0 end as speed_limit,
row_number() over (partition by vid order by date_time)
- row_number() over (
partition by vid, case when speed >= 30 then 30 else 0 end
order by date_time) as chain
from veh_speed
)
where speed_limit = 30
group by vid, chain
order by vid, start_date_time;

VID START_DATE_TIME END_DATE_TIME MAX_SPEED
---------- ------------------- ------------------- ----------
1 15/01/2014 10:00:15 15/01/2014 10:00:25 45
1 15/01/2014 10:00:35 15/01/2014 10:00:50 38

SQL Fiddle .

关于sql - 根据特定标准对记录进行分组并查找最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21134501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com