我有一个如下数据表,按数据,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。
有没有人有想法?
由于
答案 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子句中使用排序时的默认窗口。