使用sql / PHP在特定日期计算两个不同行/列中两个数字的平均值,方差和标准差

时间:2013-01-23 11:45:53

标签: mysql sql sqlite date variance

我有一个具有以下结构的数据库:

rowid       ID                  startTimestamp   endTimestamp   subject
1           00:50:c2:63:10:1a   ...1000          ...1090        entrance
2           00:50:c2:63:10:1a   ...1100          ...1270        entrance
3           00:50:c2:63:10:1a   ...1300          ...1310        door1
4           00:50:c2:63:10:1a   ...1370          ...1400        entrance
.
.
.

使用此SQL-Query,我可以得到endTime和startTime之间的平均差异,按行和ID排序,按行,最小值,方差和标准差排序:

SELECT ID,AVG(diff) AS average,
   AVG(diff*diff) - AVG(diff)*AVG(diff) AS variance,
   SQRT(AVG(diff*diff) - AVG(diff)*AVG(diff)) AS stdev,
   MIN(diff) AS minTime,
   MAX(diff) AS maxTime

FROM
(SELECT t1.id, t1.endTimestamp,
        min(t2.startTimeStamp) - t1.endTimestamp AS diff
FROM table1 t1
INNER JOIN table1 t2
ON t2.ID = t1.ID AND t2.subject = t1.subject
AND t2.startTimestamp > t1.startTimestamp  -- consider only later startTimestamps
WHERE t1.subject = 'entrance'
GROUP BY t1.id, t1.endTimestamp) AS diffs
GROUP BY ID

这很好用,如果我在同一天只有几行而且时间差异较小,你可以在这个sqlfiddle中看到它:

http://sqlfiddle.com/#!2/6de73/1

但是当我在另一天获得额外数据时,我会得到不好的价值:

http://sqlfiddle.com/#!2/920b6/1

因此,我想计算每一天的平均值,最小值,最大值,方差,标准差。

我知道有MySQL的DATE功能但是我无法完成它...有人可以帮助我吗?或者我是否必须编写一段可以处理此问题的PHP代码?

1 个答案:

答案 0 :(得分:3)

是否像在group by中添加日期一样简单。这里的语法应该适用于MySQL和SQLite,基于结束时间的日期并假设结束时间存储为日期时间:

SELECT ID, thedate, AVG(diff) AS average,
   AVG(diff*diff) - AVG(diff)*AVG(diff) AS variance,
   SQRT(AVG(diff*diff) - AVG(diff)*AVG(diff)) AS stdev,
   MIN(diff) AS minTime,
   MAX(diff) AS maxTime
FROM (SELECT t1.id, t1.endTimestamp, DATE(endtimestamp) as thedate,
             min(t2.startTimeStamp) - t1.endTimestamp AS diff
      FROM table1 t1 INNER JOIN
           table1 t2
           ON t2.ID = t1.ID AND t2.subject = t1.subject AND
              t2.startTimestamp > t1.startTimestamp  -- consider only later startTimestamps
     WHERE t1.subject = 'entrance'
     GROUP BY t1.id, t1.endTimestamp
    ) AS diffs
GROUP BY ID, thedate

如果存储为时间戳,请参阅Marty的评论。