有没有办法按此表中的时间间隔进行分组?
我有一个像这样的表:
DateTime A
10:00:01 2
10:00:07 4
10:00:10 2
10:00:17 1
10:00:18 3
是否可以创建一个查询,每 10 秒返回 A 的平均值?在这种情况下,结果将是:
3 (4+2)/2
2 (2+1+3)/3
提前致谢!
编辑:如果你真的认为这不能做到,就说“不”! :) 这是一个可以接受的答案,我真的不知道这是否可以做到。
EDIT2:我正在使用 SQL Server 2008。我希望有不同的分组但固定。例如,范围为每 10 秒、1 分钟、5 分钟、30 分钟、1 小时和 1 天(只是一个示例,但类似的情况)
I have a table like this one:
DateTime A
10:00:01 2
10:00:07 4
10:00:10 2
10:00:17 1
10:00:18 3
Is this possible to create a query that returns me the average value of A each 10 seconds? In this case the result would be:
3 (4+2)/2
2 (2+1+3)/3
Thanks in advance!
EDIT: If you really think that this can not be done just say NO WAY! :) It's an acceptable answer, I really don't know if this can be done.
EDIT2: I'm using SQL Server 2008. I would like to have different groupings but fixed. For example, ranges each 10 sec, 1 minute, 5 minutes, 30 minutes, 1 hour and 1 day (just an example but something like that)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 SQL Server 中,您可以使用 DATEPART,然后按小时、分钟和秒整数除以 10 进行分组。
In SQL Server, you can use DATEPART and then group by hour, minute and second integer-division 10.
这取决于您使用的 DBMS。
在 Oracle 中您可以执行以下操作:
It depends on DBMS you are using.
In Oracle you can do the following:
有人可能会用完整的代码给你一个答案,但我处理这个问题的方法是将其分解为几个较小的问题/解决方案:
(1)创建一个带有间隔的临时表。请参阅此问题的已接受答案:
获取两个日期之间的日期列表
这个答案是针对 MySQL 的,但应该可以帮助您入门。谷歌搜索“创建间隔 SQL”也应该会产生其他方法来实现此目的。您将需要使用主表中的 MAX(DateTime) 和 MIN(DateTime) 作为您使用的任何方法的输入(显然,跨度为 10 秒)。
(2) 将临时表与主表连接,连接条件为(伪代码):
(3) 现在应该像正确选择和分组一样简单:
编辑: 如果您想查看如果间隔没有记录(零平均值),则必须重新排列查询,使用 LEFT JOIN 并在 mA 上进行 COALESCE(见下文)。如果你不关心看到这样的内部结构,OCary 的解决方案更好/更干净。
Someone may come along and give you an answer with full code, but the way I would approach this is to break it down to several smaller problems/solutions:
(1) Create a temp table with intervals. See the accepted answer on this question:
Get a list of dates between two dates
This answer was for MySQL, but should get you started. Googling "Create intervals SQL" should also yield additional ways to accomplish this. You will want to use the MAX(DateTime) and MIN(DateTime) from your main table as inputs into whatever method you use (and 10 seconds for the span, obviously).
(2) Join the temp table with your main table, with a join condition of (pseudocode):
(3) Now it should be as simple as correctly SELECTing and GROUPing:
Edit: if you want to see intervals with that have no records (zero average), you would have to rearrage the query, use a LEFT JOIN, and COALESCE on m.A (see below). If you don't care about seeing such interals, OCary's solution is better/cleaner.
我通过使用通用表表达式来获取数据的任何给定日期之间的所有周期来解决此问题。原则上,您可以将间隔更改为任何 SQL 间隔。
I approached this by using a Common Table Expression to get all the periods between any given dates of my data. In principal you could change the interval to any SQL interval.
来自 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID =142634 您也可以使用以下查询:
然后有人扩展该查询以提出建议:
尽管我不太清楚他们计划如何在第二个建议中获得平均值。
就我个人而言,我更喜欢 OCary 的回答,因为我知道那里发生了什么,并且我将能够在 6 个月内理解它时间没有再次查找,但为了完整性我将其包括在内。
From http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=142634 you can use the following query as well:
which someone then expands upon to suggest:
although I'm not too how they plan on getting the average in in the second suggestion.
Personally I prefer OCary's answer as I know what is going on there and that I'll be able to understand it in 6 months time without looking it up again but I include this one for completeness.