表中非连续行之间的 Datediff
我想从下面的表 1 中取时间差的平均值。这些值不连续,有时时间值会重复,因此我需要 1)按时间排序,2)丢弃非唯一值,3)执行时间差(以毫秒为单位),然后 4)平均结果时间差价值观。此外,我想 5)将 datediff 操作限制在选定的时间范围内,例如 其中 _TimeStamp >= '20091220 11:59:56.1' 且 _TimeStamp <= _TimeStamp >= '20091220 11:59:56.8'。我很困惑如何将这一切组合在一起!
表1:
_时间戳
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
I would like to take the average of the time difference from Table1 below. The values are not consecutive and occasionally the time value is repeated, so I need to 1) sort by time, 2) discard non-unique values, 3) perform the time difference (in milliseconds), then 4) average the resulting time difference values. Further I'd like to 5) limit the datediff operation to a chosen time range, such as
WHERE _TimeStamp >= '20091220 11:59:56.1' AND _TimeStamp <= _TimeStamp >= '20091220 11:59:56.8'. I am pretty stumped how to put this all together!
Table1:
_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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个有效且不难看的:
将为您提供示例的以下输出:
编辑:如果您想限制时间范围,则只需在
WHERE _Timestamp BETWEEN @StartDate AND @EndDate
之前添加>GROUP BY 行。Edit2:如果您想要平均值,请将最终的 SELECT t1.dt, ... 语句更改为:
Here's one that works and is not ugly:
Will give you the following output from your example:
Edit: If you want to restrict time ranges then just add
WHERE _Timestamp BETWEEN @StartDate AND @EndDate
before theGROUP BY
line.Edit2: And if you want the average, then change that final
SELECT t1.dt, ...
statement to:步骤 1 是仅选择唯一的时间:
然后,如果您想要将所有时间相互比较(不确定您想要如何选择时间),您可以做一些疯狂的事情,例如:
我的语法可能会关闭,因为我来自 MySQL,但类似的东西应该可以工作。
如果你想要平均值,你可以尝试取上述结果的平均值:
尚未测试,但理论上,我认为它应该有效。
Step 1 is to select only unique times:
Then, if you want to, say, compare all times with each other (not sure how you want to select times), you could do something crazy like:
My syntax might be off, because I'm coming from MySQL, but something similar should work.
If you want the average, you could try taking the average of the above results:
Still untested, but in theory, I think it should work.
如果我对你想要的东西的假设是正确的,那么我会看到两种方法。
直接的方法:
棘手的方法:
毕竟,平均差异只是总差异除以您要分解的分区数。
如果您想限制日期范围,则需要为日期范围添加 WHERE 子句,并且需要考虑在第二个查询中除以零的可能性。
If my assumptions on what you want are correct, then I see two ways of doing it.
The direct way:
The tricky way:
After all, the average differences is just the total difference divided by the number of divisions into which you're breaking it down.
You'll need to add WHERE clauses for the date range if you want to limit by that and you will need to account for the possibility of divide by zero in the second query.