2

我想从下面的表 1 中取时间差的平均值。这些值不是连续的,有时时间值会重复,所以我需要 1)按时间排序,2)丢弃非唯一值,3)执行时间差(以毫秒为单位),然后 4)平均得到的时间差价值观。此外,我想 5) 将 datediff 操作限制在选定的时间范围内,例如 WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= _TimeStamp >= '20091220 11:59:56.8'。我很困惑如何把这一切放在一起!

表1:
_TimeStamp
2009-12-20 11:59:56.0
2009-12-20 11:59:56.5
2009-12-20 11:59:56.3
2009-12-20 11:59:56.4
2009-12-20 11: 59:56.4
2009-12-20 11:59:56.9

4

3 回答 3

1

第 1 步是仅选择唯一时间:

SELECT DISTINCT _TimeStamp FROM table 
    WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= '20091220 11:59:56.8';

然后,如果你想,比如说,将所有时间相互比较(不确定你想如何选择时间),你可以做一些疯狂的事情,比如:

SELECT t1._TimeStamp, t2._TimeStamp, DATEDIFF(ms,t1._TimeStamp,t2._TimeStamp) FROM 
    (SELECT DISTINCT _TimeStamp FROM table 
        WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= '20091220 11:59:56.8') AS t1 
    INNER JOIN
    (SELECT DISTINCT _TimeStamp FROM table 
        WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= '20091220 11:59:56.8') AS t2
WHERE t1._TimeStamp != t2._TimeStamp;

我的语法可能不正确,因为我来自 MySQL,但类似的东西应该可以。

如果你想要平均值,你可以尝试取上述结果的平均值:

SELECT AVG(DATEDIFF(ms,t1._TimeStamp,t2._TimeStamp)) FROM 
    (SELECT DISTINCT _TimeStamp FROM table 
        WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= '20091220 11:59:56.8') AS t1 
    INNER JOIN
    (SELECT DISTINCT _TimeStamp FROM table 
        WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= '20091220 11:59:56.8') AS t2
WHERE t1._TimeStamp != t2._TimeStamp;

仍然未经测试,但从理论上讲,我认为它应该有效。

于 2010-01-11T16:56:43.417 回答
1

如果我对您想要的假设是正确的,那么我会看到两种方法。

直接方式:

SELECT
    AVG(DATEDIFF(ms, T1.my_time, T2.my_time))
FROM
    My_Table T1
INNER JOIN My_Table T2 ON
    T2.my_time > T1.my_time
WHERE
    NOT EXISTS
    (
        SELECT
            *
        FROM
            My_Table T3
        WHERE
            (T3.my_time > T1.my_time AND T3.my_time < T2.my_time) OR
            (T3.my_time = T1.my_time AND T3.my_pk < T1.my_pk) OR
            (T3.my_time = T2.my_time AND T3.my_pk < T2.my_pk)
    )

棘手的方法:

SELECT
    DATEDIFF(ms, MIN(my_time), MAX(my_time))/(COUNT(DISTINCT my_time) - 1)
FROM
    My_Table

毕竟,平均差异只是总差异除以您将其分解的分区数。

如果要限制日期范围,则需要为日期范围添加 WHERE 子句,并且需要考虑在第二个查询中除以零的可能性。

于 2010-01-11T19:23:04.037 回答
1

这是一个有效且不难看的方法:

;WITH Time_CTE AS
(
    SELECT
        MIN(_Timestamp) AS dt,
        ROW_NUMBER() OVER (ORDER BY MIN(_Timestamp)) AS RowNum
    FROM Table1
    GROUP BY _Timestamp
)
SELECT
    t1.dt AS StartDate,
    t2.dt AS EndDate,
    DATEDIFF(MS, t1.dt, t2.dt) AS Elapsed
FROM Time_CTE t1
INNER JOIN Time_CTE t2
ON t2.RowNum = t1.RowNum + 1

将为您提供示例中的以下输出:

StartDate               | EndDate                 | Elapsed
------------------------+-------------------------+--------
2009-12-20 11:59:56.000 | 2009-12-20 11:59:56.300 | 300
2009-12-20 11:59:56.300 | 2009-12-20 11:59:56.400 | 100
2009-12-20 11:59:56.400 | 2009-12-20 11:59:56.500 | 100
2009-12-20 11:59:56.500 | 2009-12-20 11:59:56.900 | 400

编辑:如果你想限制时间范围,那么只需WHERE _Timestamp BETWEEN @StartDate AND @EndDate在该GROUP BY行之前添加。

Edit2:如果你想要平均值,那么将最后的SELECT t1.dt, ...语句更改为:

SELECT AVG(DATEDIFF(MS, t1.dt, t2.dt))
FROM Time_CTE t1 ... (same as above)
于 2010-01-11T19:39:35.770 回答