Ign*_*cia 5 sql group-by sql-server-2008
我有一张这样的桌子:
DateTime A
10:00:01 2
10:00:07 4
10:00:10 2
10:00:17 1
10:00:18 3
Run Code Online (Sandbox Code Playgroud)
这是否可以创建一个查询,每 10 秒返回一次 A 的平均值?在这种情况下,结果将是:
3 (4+2)/2
2 (2+1+3)/3
Run Code Online (Sandbox Code Playgroud)
提前致谢!
编辑:如果您真的认为这无法完成,请说“不”!:) 这是一个可以接受的答案,我真的不知道这是否可以做到。
EDIT2:我使用的是 SQL Server 2008。我想要不同的分组但已修复。例如,范围每 10 秒、1 分钟、5 分钟、30 分钟、1 小时和 1 天(只是一个例子,但类似的东西)
在 SQL Server 中,您可以使用 DATEPART,然后按小时、分钟和秒整除 10 进行分组。
CREATE TABLE #times
(
thetime time,
A int
)
INSERT #times
VALUES ('10:00:01', 2)
INSERT #times
VALUES ('10:00:07', 4)
INSERT #times
VALUES ('10:00:10', 2)
INSERT #times
VALUES ('10:00:17', 1)
INSERT #times
VALUES ('10:00:18', 3)
SELECT avg(A) -- <-- here you might deal with precision issues if you need non-integer results. eg: (avg(a * 1.0)
FROM #times
GROUP BY datepart(hour, thetime), DATEPART(minute, thetime), DATEPART(SECOND, thetime) / 10
DROP TABLE #times
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11984 次 |
| 最近记录: |