使用SQL server 2008R2我正在尝试编写一个查询,该查询将返回特定时间范围内所有值的总和(在这种情况下,间隔为15分钟)从长远来看,这将用于识别缺失的数据(每个服务器应该返回每15分钟12的值)。我已经到了我得到的结果,我正在寻找但我需要一种方法将它们全部组合在同一个查询中。下面的代码(日期通常由变量定义,在这种情况下使用09/01/2013作为示例):
Select SUM(Accounted_for) 'accounted for',date,time,server
from dbmetrics.dbo.tblmissing
where DATE='09/01/2013'
and (TIME between '00:00:00' and '23:59:59')
and SERVER='Server01'
group by DATE, TIME, server , DATEPART(hour,time),
DATEPART(MINUTE,time),DATEPART(second,time) / 96
GO
结果沿着这条线回归“ 回来的结果看起来像这样
Number Date Time Server
4 2013-09-01 12:00:01.0000000 server01
6 2013-09-01 12:00:02.0000000 server01
2 2013-09-01 12:00:15.0000000 server01
10 2013-09-01 12:15:02.0000000 server01
2 2013-09-01 12:15:09.0000000 server01
我希望看到的是
12 2013-09-01 12:00:00.0000 Server01
12 2013-09-01 12:15:00.0000 Server01
答案 0 :(得分:1)
您可能希望GROUP BY
[Server]
而不是获得MAX()
[Server]
。正如Aaron Bertrand提到的那样,只是你的时间值以15分钟的间隔着陆。此查询将分钟处理时间并将数据分组。
SELECT SUM([Number]) AS [Number],
[Date],
CONVERT(time, DATEADD(minute, DATEDIFF(minute, 0, [Time]), 0)) AS [Time],
MAX([Server])
FROM [Table]
GROUP BY [Date],
DATEADD(minute, DATEDIFF(minute, 0, [Time]), 0)