简体   繁体   English

SQL查询基于时间戳计算时间跨度

[英]SQL query to calculate time spans based upon time stamps

In my project, I have need of calculating a time span. 在我的项目中,我需要计算时间跨度。 Currently, I'm retrieving every TimeStamp that matches my query and storing them in a List<> . 当前,我正在检索与查询匹配的每个时间戳,并将它们存储在List<> Then, I iterate through the list to see if any intervals are 10 seconds or less, and then I add those together. 然后,我遍历列表以查看是否有10秒或更短的时间间隔,然后将它们加在一起。 Anything greater than 10 seconds is ignored. 大于10秒的时间将被忽略。 I'm wondering if there is a SQL query that I can do that will do this for me? 我想知道是否存在可以执行的SQL查询,对我来说可以吗? I've done some searching, but didn't find anything. 我已经做了一些搜索,但是什么也没找到。 Essentially, I'd like to not have to store so much information in memory if I don't have to. 本质上,如果不需要,我不想在内存中存储太多信息。 Here's the method I'm using to iterate through my List<> : 这是我用来遍历List<>

private static TimeSpan TimeCalculations(IList<DateTime> timeStamps)
{
    var interval = new TimeSpan(0, 0, 10);
    var totalTime = new TimeSpan();

    for (var j = 0; j < timeStamps.Count - 1; j++)
    {
        if (timeStamps[j + 1].Subtract(timeStamps[j]) > interval) continue;
        var timeDifference = timeStamps[j + 1].Subtract(timeStamps[j]);
        totalTime = totalTime.Add(timeDifference);
    }

    return totalTime;
}

The data that is being retrieved currently can be anywhere from 10 to 400k rows worth of data. 当前正在检索的数据可以是10到40万行的数据。 Here is a sample: 这是一个示例:

2006-09-07 11:46:09
2006-09-07 11:46:19 - 10 seconds
2006-09-07 11:46:20 - 1 second
2006-09-07 11:46:36

2006-09-07 11:47:49
2006-09-07 11:47:53 - 4 seconds
2006-09-07 11:48:02 - 9 seconds
2006-09-07 11:48:15
2006-09-07 11:48:29
2006-09-07 11:48:34 - 5 seconds

2006-09-07 11:54:29
2006-09-07 11:54:39 - 10 seconds
2006-09-07 11:54:49 - 10 seconds
2006-09-07 11:54:59 - 10 seconds

This would result in about 59 seconds. 这将导致大约59秒。 This is the kind of result I'm looking for. 这是我想要的结果。

The database I'm using is SQLite. 我正在使用的数据库是SQLite。

EDIT 编辑

Looking at the answers, I can tell that my question wasn't quite thorough enough. 查看答案,我可以说我的问题还不够彻底。 My current query to get the TimeStamps is sufficient. 我当前获取时间戳的查询就足够了。 What I'm looking for is a query to add the difference between them together, if the interval is 10 seconds or less. 我要寻找的是一个查询,如果间隔为10秒或更短,则将它们之间的差异相加。

Used your sample data to create a sqlfiddle and this query works against your sample data: 使用您的样本数据创建了一个sqlfiddle ,此查询针对您的样本数据起作用:

SELECT DISTINCT tbl.timestamp FROM main_tbl tbl
INNER JOIN
(
 SELECT temp.ID, temp.timestamp FROM main_tbl temp
)test 
ON tbl.timestamp <= datetime(test.timestamp, '+10 seconds')
AND tbl.timestamp >= datetime(test.timestamp, '-10 seconds')
AND tbl.ID <> test.ID
ORDER BY tbl.timestamp

http://sqlfiddle.com/#!7/049f5/3 http://sqlfiddle.com/#!7/049f5/3

EDIT 2: 编辑2:

SELECT
sum(
  strftime('%s',
    (
    SELECT min(temp.timestamp)
        FROM main_tbl temp
        WHERE temp.timestamp > tbl.timestamp
    )
  ) - strftime('%s',tbl.timestamp)
) as total_sum
FROM main_tbl tbl
WHERE (
  strftime('%s',
    (
      SELECT min(temp.timestamp)
      FROM main_tbl temp
      WHERE temp.timestamp > tbl.timestamp
    )
  ) - strftime('%s',tbl.timestamp)
) <= 10 
AND date = "2013-05-13" 
AND col1 = col2

http://sqlfiddle.com/#!7/049f5/55 http://sqlfiddle.com/#!7/049f5/55

I played with a simple table with int cols t1 and t2 and got the right results from this query, I think. 我认为我玩了一个具有int col t1和t2的简单表,并从此查询中获得了正确的结果。 Adapt the penultimate line as needed! 根据需要调整倒数第二行!

SELECT sum(diff) FROM ( 
SELECT  t_1.rowid AS this_id, other_t1 - t_1.t1 as diff
FROM temp AS t_1
JOIN
 (SELECT t_2.t1 AS other_t1, t_2.rowid AS other_id
  FROM temp t_2  )
ON this_id = other_id-1
WHERE other_t1 - t_1.t1 = 1
);

It's a triple-nested select. 这是一个三重嵌套的选择。 The outer one sums all the differences found. 最外面的一个总结了所有发现的差异。 and is just the first and last lines. 只是第一行和最后一行。 The second level - from the 2nd line - does most of the work. 第二层-从第二行开始-完成大部分工作。 The inner select delivers a list of table row and timestamp values for the second level to play with. 内部选择为第二级播放提供了表行和时间戳值的列表。

The action is to sum the differences of all rows that have a "t1" that differs by 1 from the next higher row. 动作是将所有具有“ t1”的行的差与下一个较高的行相加1。

To see the differences themselves, omit the first and last lines and replace the ";" 要查看差异本身,请省略第一行和最后一行,并替换为“;”

... forgot to say. ...忘了说。 t1 was supposed to be the timestamp. t1应该是时间戳记。 t2 was representing "other data". t2代表“其他数据”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM