我有一个名为LOGENTRY的简单表,其中包含名为“DATE”和“COST”的字段。例如:
+--------------+-------+
| DATE | COST |
+--------------+-------+
| MAY 1 2013 | 0.8 |
| SEP 1 2013 | 0.4 |
| NOV 1 2013 | 0.6 |
| DEC 1 2013 | 0.2 |
+--------------+-------+
我想找到COST字段随时间的斜率(选择的行数范围),得到 SLOPE = -0.00216(这相当于Excel的SLOPE函数,即线性回归)。
有没有一种简单的方法来选择COST的斜率?如果我用调用语言(php)进行数学计算,我可以找到斜率:
SLOPE = (N * Sum_XY - Sum_X * Sum_Y)/(N * Sum_X2 - Sum_X * Sum_X);
我看到一些类似的问题,但它们更复杂。我试图将这个例子剥离到最简单的情况 - 所以我能理解答案:)这里和我一样接近......但是MYSQL抱怨附近的语法: 'float))AS Sum_X,SUM(CAST(LOGENTRY.DATE as float)* CAST(LOGENTRY.DATE'
SELECT
COUNT( * ) AS N,
SUM( CAST( LOGENTRY.DATE AS FLOAT ) ) AS Sum_X,
SUM( CAST( LOGENTRY.DATE AS FLOAT ) * CAST( LOGENTRY.DATE AS FLOAT ) ) AS Sum_X2,
SUM( LOGENTRY.COST ) AS Sum_Y, SUM( LOGENTRY.COST * LOGENTRY.COST ) AS Sum_Y2,
SUM( CAST( LOGENTRY.DATE AS FLOAT ) * LOGENTRY.COST ) AS Sum_XY
FROM LOGENTRY
答案 0 :(得分:4)
似乎MySQL无法将日期转换为float(根据stackoverflow中的其他示例)。也许其他示例引用另一个数据库。因此,通过将日期转换为unix_timestamps,我能够得到答案......在PHP中进行最终计算。如果这是错误的...请发布,我将删除答案...
SELECT
COUNT(*) AS N,
SUM(UNIX_TIMESTAMP(LOGENTRY.DATE)) AS Sum_X,
SUM(UNIX_TIMESTAMP(LOGENTRY.DATE) * UNIX_TIMESTAMP(LOGENTRY.DATE)) AS Sum_X2,
SUM(LOGENTRY.COST) AS Sum_Y,
SUM(LOGENTRY.COST*LOGENTRY.COST) AS Sum_Y2,
SUM(UNIX_TIMESTAMP(LOGENTRY.DATE) * LOGENTRY.COST) AS Sum_XY
FROM LOGENTRY