如何在SQL Server中使用线性回归预测未来值

时间:2018-11-13 13:43:09

标签: sql sql-server statistics regression trendline

我当前的查询如下:

select 
    b0 + x * b1 as trend,
    date 
from
    (select 
         (n * sumxy - (sumx * sumy)) / (n * sumxx - sumx2) as b1,
         (sumy * sumxx - sumx * sumxy) / (n * sumxx - sumx2) as b0,
         x,
         date
     from
         (select 
              sum(x) over() as sumx,
              sum(x) over() * sum(x) over() as sumx2,
              sum(y) over() as sumy,
              y,
              x,
              date,
              N,
              sum(xx) over() as sumxx,
              sum(x*y) over() as sumxy
          from
              (select 
                   utilization as y,
                   row_number() over(order by cryear, crmonth) as x,
                   row_number() over(order by cryear, crmonth) * row_number() over(order by cryear, crmonth) as xx,
                   datefromparts(cryear, crmonth, 1) as date,
                   count(crmonth) over() as N
               from
                   test) sub1
           group by 
               x, y, date, n, xx) sub2) sub3

返回以下结果:(趋势线的y和x坐标) 编辑:可能会给出不同的趋势值,因为我只是在虚拟数据库中弥补了利用率值

trend               date
0.431791238486767   2017-01-01
0.430082148360641   2017-02-01
0.428373058234514   2017-03-01
0.426663968108388   2017-04-01

测试表中的原始数据如下

cryear | crmonth | utilization value
2017   |   01    | 0,43
2017   |   02    | 0,45  
2017   |   03    | 0,52 
2017   |   04    | 0,48

等 等


我不知道我创建的算法是否能够预测尚不知道利用率值的月份的趋势值。

例如,2017-05年没有利用率值,无论如何,我是否可以通过某种方式修改脚本以为其计算趋势值?我真的很困,无法想到任何有用的东西。

虚拟数据库:

create table test 
(
    CRYEAR      SMALLINT,
    CRMONTH     SMALLINT,
    UTILIZATION FLOAT(24)
);

Insert into test (cryear, crmonth, utilization) 
values ('2017', '01', '0.43'), ('2017', '02', '0.45'),
       ('2017', '03', '0.52'), ('2017', '04', '0.48');

在此先感谢您,并感谢您的来信

0 个答案:

没有答案