地铁数据 列:
答案 0 :(得分:2)
您可以使用累积总和的差异来执行此操作。诀窍在于通过计算station_id
已经1
到该记录的次数来识别我所做的组。
select s.*,
(sum(boarded) over (partition by grp order by id) -
sum(deboarded) over (partition by grp order by id)
) as occupants
from (select s.*,
count(*) filter (where station_id = 1) over (order by id) as grp
from subwaydata s
) s;