您启发了我去在SQL Server中实现线性回归。可以对MySQL / Oracle /
Whatever进行修改,而不会带来太多麻烦。这是确定每个entity_id在一小时内趋势的数学上最好的方法,并且只会选择趋势为正的趋势。
它实现了此处列出的用于计算B1hat的公式:https
://en.wikipedia.org/wiki/Regression_analysis#Linear_regression
create table #temp( entity_id int, value int, [date] datetime)insert into #temp (entity_id, value, [date])values(1,10,'20140102 07:00:00 AM'),(1,20,'20140102 07:15:00 AM'),(1,30,'20140102 07:30:00 AM'),(2,50,'20140102 07:00:00 AM'),(2,20,'20140102 07:47:00 AM'),(3,40,'20140102 07:00:00 AM'),(3,40,'20140102 07:52:00 AM')select entity_id, 1.0*sum((x-xbar)*(y-ybar))/sum((x-xbar)*(x-xbar)) as Betafrom( select entity_id, avg(value) over(partition by entity_id) as ybar, value as y, avg(datediff(second,'20140102 07:00:00 AM',[date])) over(partition by entity_id) as xbar, datediff(second,'20140102 07:00:00 AM',[date]) as x from #temp where [date]>='20140102 07:00:00 AM' and [date]<'20140102 08:00:00 AM') as Calcsgroup by entity_idhaving 1.0*sum((x-xbar)*(y-ybar))/sum((x-xbar)*(x-xbar))>0



