我的数据整理如下表T_FORECAST
Location Sublocation Delivery_forecast Delivery_Date Forecast_date
----------------------------------------------------------------------------------
1 1 100 2020-01-01 2019-01-01
1 2 50 2020-01-01 2019-05-01
1 1 90 2020-01-01 2019-06-01
1 2 70 2020-01-01 2019-10-01
. . .
我正在尝试编写一个查询,该查询将输出每个位置的Delivery_forecast,Delivery_date和Forecast_date的总和。在下面的示例中,我期望:
Location Delivery_forecast Delivery_Date Forecast_date
----------------------------------------------------------------------------------
1 100 2020-01-01 2019-01-01
1 150 2020-01-01 2019-05-01
1 140 2020-01-01 2019-06-01
1 160 2020-01-01 2019-10-01
我可以使用下面的请求找到我需要的行的列表,但是我找不到找到正确总和的方法。我相信我必须做一个自我加入
SELECT DISTINCT f.Location, f.Delivery_Date, f.Forecast_date
FROM T_FORECAST f
使用累计金额:
select f.*,
sum(delivery_forecast) over (partition by location, delivery_date order by forecast_date) as running_delivery_forecast
from T_FORECAST f;