4

Is there a way we can generate a time series forecasting for a data set using an Oracle analytical functions? How do we perform extrapolation in 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  ?
3
  • You need to tell us which logic/model you want to use for extrapolation. Or, are you asking more generally if Oracle already has some sort of package for doing this? Mar 13, 2019 at 5:02
  • @Tim Biegeleisen just curious on available options.. I personally prefer linear regression with polynomial fitting Mar 13, 2019 at 5:53
  • 1
    Well you should have included that in your question before others attempted answers using other approaches. Mar 13, 2019 at 5:57

1 Answer 1

3

You can create a simple forecast using the REGR linear regression functions.

--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.

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". If you want to restrict the forecasts to always stay at or above 0, a CASE expression may help:

--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
6
  • Thank you, Do we have a polynomial fit function in Oracle? like curve fitting with degree 1 (linear regress)?? to get better accuracy?? Mar 13, 2019 at 5:46
  • @sunnybabau Not that I know of. The package DBMS_STAT_FUNCS has functions to determine if the sample fits, but it doesn't forecast. Maybe there's something in the Data Mining APIs I don't know about. I'm not very knowledgeable about statistics. The two times I've seen forecasting done professionally, the above approach was the only one used. (Seems like most forecasting is 99% exception handling, 1% formulas.)
    – Jon Heller
    Mar 13, 2019 at 5:59
  • Just wanted to thank you for the above. I'm actively using the above logic. Also, if i understand i see some negative values as forecast, which i believe resembles slope line is downward correct? Any way to avoid this negative value i have used abs() to the forecast data set. IS that ok? However, does this also mean the data is far from the slope line and our forecast is not accurate?? May 23, 2019 at 22:28
  • @sunnybabau See my edited answer. I don't think ABS would be a good idea, but limiting the answer to a minimum of 0 might work better. In my limited experience with forecasting, the equations were the simple part. Most of the effort goes into managing all the exceptions.
    – Jon Heller
    May 24, 2019 at 3:50
  • Thank you for your response sir. But If you see my sample set (edited in my original question). The foretasted value is less than prior, which implies negative slope resulting a negative value, so i used abs() to avoid. The forecast makes sense but negative doesn't make sense. Please advice. Also i wish Oracle provides some way to curve fitting techniques to improve the forecast. Any alternative around how to perform curve fitting for the above forested values ? May 24, 2019 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.