查找数据集中每个走廊的最大英里参考
Find the max mile reference for each corridor within dataset
我正在尝试查找数据集中每个走廊的最大里程数走廊段。我使用这个非常相似的查询来查找最小里程数,或者在增加方向上的走廊上的第一段,并且效果很好:
select distinct t.corridor_code_rb,t.frfpost,t.trfpost
from SEC_SEGMENTS t
where t.dir = 'I' and t.lane = 1
and t.frfpost = (select min(s.frfpost) from SEC_SEGMENTS s)
order by 1,2
但是,当我尝试使用类似的查询通过以下查询查找最大(增加方向的最后一段)走廊长度时,问题就出现了:
select distinct t.corridor_code_rb,t.frfpost,t.trfpost
from SEC_SEGMENTS t
where t.dir = 'I' and t.lane = 1
and t.trfpost = (select max(s.trfpost) from SEC_SEGMENTS s)
group by t.corridor_code_rb,t.frfpost,t.trfpost
当我 运行 这个查询时会发生什么,它只输出第一条走廊的最高里程段,然后停止。而对于最低里程查询,它 returns 输出每个走廊,这正是我想要的。 frfpost 是每个路段的起点里程,trfpost 是终点里程。所以 frfpost 是 'from reference post' 而 trfpost 是 'to reference post'。每条走廊被分成 5 到 40 英里长的部分,通常位于与其他走廊的交界处。我正在尝试找到每条走廊的最后一段,这就是问题所在。
您还需要 group by
corridor_code_rb,才能根据 corridor_code_rb 获取该列的 max
值。然后join
它到主table.
select t.corridor_code_rb,t.frfpost,t.trfpost
from SEC_SEGMENTS t
join (select corridor_code_rb, max(s.trfpost) as trfpost from SEC_SEGMENTS
group by corridor_code_rb) s
on t.trfpost = s.trfpost
where t.dir = 'I' and t.lane = 1
根据您的评论,您似乎使用了支持分析函数的 Oracle:
Select corridor_code_rb, frfpost, tropfst
from
(
select corridor_code_rb, frfpost, trfpost,
ROW_NUMBER() -- rank the rows
OVER (PARTITION BY corridor_code_rb -- for each corridor
ORDER BY trfpost DESC) AS rn -- by descending direction
from SEC_SEGMENTS t
where t.dir = 'I' and t.lane = 1
) dt
WHERE rn = 1 -- filter the last row
我正在尝试查找数据集中每个走廊的最大里程数走廊段。我使用这个非常相似的查询来查找最小里程数,或者在增加方向上的走廊上的第一段,并且效果很好:
select distinct t.corridor_code_rb,t.frfpost,t.trfpost
from SEC_SEGMENTS t
where t.dir = 'I' and t.lane = 1
and t.frfpost = (select min(s.frfpost) from SEC_SEGMENTS s)
order by 1,2
但是,当我尝试使用类似的查询通过以下查询查找最大(增加方向的最后一段)走廊长度时,问题就出现了:
select distinct t.corridor_code_rb,t.frfpost,t.trfpost
from SEC_SEGMENTS t
where t.dir = 'I' and t.lane = 1
and t.trfpost = (select max(s.trfpost) from SEC_SEGMENTS s)
group by t.corridor_code_rb,t.frfpost,t.trfpost
当我 运行 这个查询时会发生什么,它只输出第一条走廊的最高里程段,然后停止。而对于最低里程查询,它 returns 输出每个走廊,这正是我想要的。 frfpost 是每个路段的起点里程,trfpost 是终点里程。所以 frfpost 是 'from reference post' 而 trfpost 是 'to reference post'。每条走廊被分成 5 到 40 英里长的部分,通常位于与其他走廊的交界处。我正在尝试找到每条走廊的最后一段,这就是问题所在。
您还需要 group by
corridor_code_rb,才能根据 corridor_code_rb 获取该列的 max
值。然后join
它到主table.
select t.corridor_code_rb,t.frfpost,t.trfpost
from SEC_SEGMENTS t
join (select corridor_code_rb, max(s.trfpost) as trfpost from SEC_SEGMENTS
group by corridor_code_rb) s
on t.trfpost = s.trfpost
where t.dir = 'I' and t.lane = 1
根据您的评论,您似乎使用了支持分析函数的 Oracle:
Select corridor_code_rb, frfpost, tropfst
from
(
select corridor_code_rb, frfpost, trfpost,
ROW_NUMBER() -- rank the rows
OVER (PARTITION BY corridor_code_rb -- for each corridor
ORDER BY trfpost DESC) AS rn -- by descending direction
from SEC_SEGMENTS t
where t.dir = 'I' and t.lane = 1
) dt
WHERE rn = 1 -- filter the last row