简体   繁体   English

在 Oracle/SQL 中预测时间序列数据

[英]Forecasting Time series data in Oracle/SQL

Is there a way we can generate a time series forecasting for a data set using an Oracle analytical functions?有没有一种方法可以使用 Oracle 分析函数为数据集生成时间序列预测? How do we perform extrapolation in SQL/ORACLE.我们如何在 SQL/ORACLE 中执行外推。

Below is my need下面是我的需要

I have data data set like below and I wanted to forecast/extrapolate for next year我有如下数据集,我想预测/推断明年

Cust_id  Year  Revnue
1        2016  679862
1        2017  705365
1        2018  ?
2        2016  51074
2        2017  50611
2        2018  ?
3        2016  190706
3        2017  90393
3        2018  ?
4        2016  31649
4        2017  19566
4        2018  ?

You can create a simple forecast using the REGR linear regression functions.您可以使用REGR线性回归函数创建一个简单的预测。

--Ordinary least squares forecast for each customer for the next year.
select
    cust_id,
    max(year) +1 forecast_year,
    -- y = mx+b
    regr_slope(revenue, year)
        * (max(year) + 1)
        + regr_intercept(revenue, year) forecasted_revenue
from customer_data
group by cust_id;

CUST_ID   FORECAST_YEAR   FORECASTED_REVENUE
-------   -------------   ------------------
1                  2018               730868
2                  2018                50148
4                  2018                 7483
3                  2018                -9920

Below is the sample schema.下面是示例架构。 Or you can use this SQLFiddle .或者你可以使用这个 SQLFiddle

create table customer_data
(
    cust_id number,
    year number,
    revenue number
);

insert into customer_data
select 1, 2016, 679862 from dual union all
select 1, 2017, 705365 from dual union all
select 2, 2016, 51074  from dual union all
select 2, 2017, 50611  from dual union all
select 3, 2016, 190706 from dual union all
select 3, 2017, 90393  from dual union all
select 4, 2016, 31649  from dual union all
select 4, 2017, 19566  from dual;

The REGR function deals with number pairs, it doesn't understand business rules like "revenue can't be below 0". REGR函数处理数字对,它不理解“收入不能低于 0”这样的业务规则。 If you want to restrict the forecasts to always stay at or above 0, a CASE expression may help:如果您想将预测限制为始终保持在 0 或以上,则CASE表达式可能会有所帮助:

--Forecasted revenue, with minimum forecast of 0.
select cust_id, forecast_year,
    case when forecasted_revenue < 0 then 0 else forecasted_revenue end forecasted_revenue
from
(
    --Ordinary least squares forecast for each customer for the next year.
    select
        cust_id,
        max(year) +1 forecast_year,
        -- y = mx+b
        regr_slope(revenue, year)
            * (max(year) + 1)
            + regr_intercept(revenue, year) forecasted_revenue
    from customer_data
    group by cust_id
);

CUST_ID   FORECAST_YEAR   FORECASTED_REVENUE
-------   -------------   ------------------
1                  2018               730868
2                  2018                50148
4                  2018                 7483
3                  2018                    0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM