6

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

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
.
.
.

我在这里准备了一个 sqlfiddle:http ://sqlfiddle.com/#!2/fe8c6/2

使用此 SQL 查询,我可以获得一行和下一行之间 endTime 和 startTime 之间的平均差异,按主题和 ID 排序:

SELECT
    id,
    ( MAX(endtimestamp) - MIN(startTimestamp)
    - SUM(endtimestamp-startTimestamp)
    ) / (COUNT(*)-1) AS averageDifference
FROM
    table1
WHERE ID = '00:50:c2:63:10:1a'
AND subject = 'entrance'
GROUP BY id;

我的问题:计算平均值没问题,就是这样查询。但是我怎样才能得到这个值的标准偏差和方差呢?

4

3 回答 3

3

首先通过将表连接到自身并按 ID 分组来找到感兴趣的时间差,然后找到平均值、方差V(x) = E(x^2) - (E(x))^2和标准差sqrt(V)

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
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
于 2013-01-20T21:21:04.040 回答
3

对于比简单求和更复杂的公式,您必须通过查找相应的下一个开始时间来计算每条记录的实际差值,如下所示:

SELECT (SELECT MIN(startTimestamp)
        FROM table1 AS next
        WHERE next.startTimestamp > table1.startTimestamp
          AND ID = '...'
       ) - endTimestamp AS timeDifference
FROM table1
WHERE nextStartTimestamp IS NOT NULL
  AND ID = '...'

然后您可以使用所有差异值进行计算:

SELECT SUM(timeDifference) / COUNT(*) AS average,
       AVG(timeDifference)            AS moreEfficientAverage,
       SUM(timeDifference * timeDifference) / COUNT(*) -
       AVG(timeDifference) * AVG(timeDifference) AS variance
FROM (SELECT (SELECT MIN(startTimestamp)
              FROM table1 AS next
              WHERE next.startTimestamp > table1.startTimestamp
                AND next.ID = '...'
             ) - endTimestamp AS timeDifference
      FROM table1
      WHERE nextStartTimestamp IS NOT NULL
        AND ID = '...')
于 2013-01-20T20:53:01.757 回答
1

几点:

  1. 你的平均值公式是错误的,正确的公式是SUM(endtimestamp-starttimestamp)/COUNT(endtimestamp)。我不知道你为什么有这些MIN/MAX条款。COUNT(*)将计算NULL行数并给出错误的结果。
  2. SQLlite 有一个avg找到平均值的函数。
  3. 方差的公式是SUM((endtimestamp-starttimestamp)*(endtimestamp-starttimestamp)) - AVG(endtimestamp-starttimestamp)*AVG(endtimestamp-starttimestamp)
  4. 标准差是方差的平方根。

作为对问题作者评论的回应,为了计算方差,必须通过自连接将开始时间和结束时间相互配对。

由于 SQL lite 中没有 row_number 函数,这有点不雅。

SELECT id,
       AVG(startTimestamp-endTimestamp) as mean,
       SUM((startTimestamp-endTimestamp)^2) - AVG(startTimestamp-endTimestamp)^2 as variance,
       SQRT(SUM((startTimestamp-endTimestamp)^2) - AVG(startTimestamp-endTimestamp)^2) as stDev
FROM
    (SELECT
        t1.id,
        t1.endTimestamp,
        MIN(t2.startTimestamp) as starttimestamp
    FROM table1 t1
         INNER JOIN
         table1 t2 ON t1.endTimestamp<=t2.startTimestamp
    GROUP BY t1.id, t1.endTimestamp) t
GROUP BY id;

请参阅SQL 小提琴

于 2013-01-20T20:58:37.330 回答