2

我有一个名为 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 作为浮点数) * 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
4

1 回答 1

4

似乎 MySQL 不能将日期转换为浮点数(根据 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
于 2013-02-15T17:50:23.387 回答