PL / SQL自回归预测

问题描述 投票:0回答:1

请原谅我,因为我不太擅长PL / SQL(或对此事进行预测)。

我希望有人能够帮助我解决PL / SQL中的预测问题/方法。

对于这个特定的预测,可以说我们需要从每个生产工厂为每个产品线生成4周(从今天/给定日期)的预测值。

我面临的困难是,我唯一的部署方式是Oracle数据库pacakge /存储过程。

我对在R中使用ARIMA模型(和VARIMA变体非常满意,并且想使用类似的东西(最佳情况下开发出类似于auto.arima方法的东西),但是受到我对PL的了解的限制。 / SQL

这里是一个虚拟表(填充有虚拟数据):

create table production_data
(
    product_id number,
    facility_id number,
    week_in_year number,
    prod_year number,
    units number
);

insert into production_data
select 1, 1, 1, 2019, 679862 from dual union all
select 1, 2, 1, 2019, 512345 from dual union all
select 1, 2, 2, 2019, 640000 from dual union all
select 2, 1, 1, 2019, 680000 from dual union all
select 2, 2, 1, 2019, 700000 from dual union all
select 2, 2, 2, 2019, 705365 from dual;

这是我到目前为止(不多):

 --Ordinary least squares forecast for each product for each facility.
select
    product_id,
    facility_id,
    prod_year,
    max(week_in_year) + 1 forecast_end,
    -- y = mx+b
    regr_slope(units, prod_year)
        * (max(week_in_year) + 1)
        + regr_intercept(units, prod_year) forecasted_units
from production_data
group by 
    product_id,
    facility_id,
    prod_year
UNION ALL 
select
    product_id,
    facility_id,
    prod_year,
    max(week_in_year) + 2 forecast_end,
    -- y = mx+b
    regr_slope(units, prod_year)
        * (max(week_in_year) + 2)
        + regr_intercept(units, prod_year) forecasted_units
from production_data
group by 
    product_id,
    facility_id,
    prod_year
UNION ALL 
select
    product_id,
    facility_id,
    prod_year,
    max(week_in_year) + 3 forecast_end,
    -- y = mx+b
    regr_slope(units, prod_year)
        * (max(week_in_year) + 3)
        + regr_intercept(units, prod_year) forecasted_units
from production_data
group by 
    product_id,
    facility_id,
    prod_year
UNION ALL 
select
    product_id,
    facility_id,
    prod_year,
    max(week_in_year) + 4 forecast_end,
    -- y = mx+b
    regr_slope(units, prod_year)
        * (max(week_in_year) + 4)
        + regr_intercept(units, prod_year) forecasted_units
from production_data
group by 
    product_id,
    facility_id,
    prod_year;
sql oracle oracle11g forecasting arima
1个回答
1
投票

将原始表与包含双重表的表交叉连接

select from dual connect by level <= 4 * 7

语法从当天开始,在接下来的四个星期中生成行:

select distinct 
       product_id,
       facility_id,
       prod_year,
       max(w.week_in_year) forecast_end,
       regr_slope(units, prod_year)
        * (max(w.week_in_year))
        + regr_intercept(units, prod_year) forecasted_units
 from production_data
cross join 
      (
       select to_char(sysdate+level-1,'iw') as week_in_year, level - 1 as lvl
         from dual
      connect by level <= 4 * 7
      ) w
group by product_id, facility_id, prod_year, lvl
order by forecast_end
© www.soinside.com 2019 - 2024. All rights reserved.