如何获取所有其余行(包括当前行)BY类别的滚动MIN编号

时间:2017-12-18 18:11:15

标签: sql oracle

我有一个如下数据表,按数据,route_number和序列排序。

Delivery Date   Order_ID    Route_Number    Stop #  Sequence    Min Stop#   Formula 
12/11/2017         Z11         100201         2        1           1    MIN(D2:$D$6)
12/11/2017         Z12         100201         1        2           1    MIN(D3:$D$6)
12/11/2017         Z13         100201         3        3           3    MIN(D4:$D$6)
12/11/2017         Z14         100201         5        4           4    MIN(D5:$D$6)
12/11/2017         Z15         100201         4        5           4    MIN(D6:$D$6)

我想要做的是在我的SQL查询中,如何在excel中获得 Min Stop#列。

逻辑是:给我一个最小停止#从当前行到同一个route_number中的所有其余行,以及交货日期,我在想像Partition by delivery_date,route_number。

有没有人有想法?

由于

2 个答案:

答案 0 :(得分:1)

使用min窗口功能。

select t.*,min(stop) over(partition by route_number,delivery_date
                          order by sequence rows between current row 
                          and unbounded following) as min_stop
from tbl t

答案 1 :(得分:0)

min(stop) over (partition by route_number, delivery_date
order by sequence rows between current row and unbounded following)

min(stop) over (partition by route_number, delivery_date
order by sequence desc rows between unbounded preceding and current row)

可以简化为

min(stop) over (partition by route_number, delivery_date
order by sequence desc) m2

因为rows between unbounded preceding and current row是在over子句中使用排序时的默认窗口。