如何修复 regr_slope() 和 regr_intercept() 返回空 PostgreSQL?
How to fix regr_slope() and regr_intercept() returning null PostgreSQL?
我 select 来自 table 的值的总和,然后他们按日期对它们进行分组。这是结果。
CREATE TABLE thedata
(
sum_values INTEGER NOT NULL
, date DATE NOT NULL
);
INSERT INTO thedata(sum_values,date) VALUES
(12 ,'2020-11-20' )
,(12 ,'2020-11-21' )
,(12 ,'2020-11-22' )
,(14 ,'2020-11-23' )
,(15 ,'2020-11-24' )
,(18 ,'2020-11-25' )
,(19 ,'2020-11-26' )
然后我用这个查询计算回归斜率和截点:
select date,
regr_slope(sum_values, extract(epoch from date)),
regr_intercept(sum_values, extract(epoch from date))
from thedata
group by date
order by date
但是,我得到 NULL
个值作为结果。 说是版本问题。我的版本是10.0
,所以可能确实是这样,但是我负担不起更新到更新的版本。如何修复斜率和截距计算?
我只需要从 select 语句中删除日期,因为它会干扰函数:
select regr_slope(sum_values, extract(epoch from date)),
regr_intercept(sum_values, extract(epoch from date))
from thedata
regr_slope regr_intercept
0.000018696581196581197 -30013.098901098903
我 select 来自 table 的值的总和,然后他们按日期对它们进行分组。这是结果。
CREATE TABLE thedata
(
sum_values INTEGER NOT NULL
, date DATE NOT NULL
);
INSERT INTO thedata(sum_values,date) VALUES
(12 ,'2020-11-20' )
,(12 ,'2020-11-21' )
,(12 ,'2020-11-22' )
,(14 ,'2020-11-23' )
,(15 ,'2020-11-24' )
,(18 ,'2020-11-25' )
,(19 ,'2020-11-26' )
然后我用这个查询计算回归斜率和截点:
select date,
regr_slope(sum_values, extract(epoch from date)),
regr_intercept(sum_values, extract(epoch from date))
from thedata
group by date
order by date
但是,我得到 NULL
个值作为结果。 10.0
,所以可能确实是这样,但是我负担不起更新到更新的版本。如何修复斜率和截距计算?
我只需要从 select 语句中删除日期,因为它会干扰函数:
select regr_slope(sum_values, extract(epoch from date)),
regr_intercept(sum_values, extract(epoch from date))
from thedata
regr_slope regr_intercept
0.000018696581196581197 -30013.098901098903