1

这类似于但不等于我之前的问题 那是关于如何每天总结日志项目。我使用这个 SQL。

SELECT 
  [DateLog] = CONVERT(DATE, LogDate), 
  [Sum] = COUNT(*)
FROM PerfRow
GROUP BY CONVERT(DATE, LogDate)
ORDER BY [DateLog];

现在我想改进它以在任意时间段内进行总结。所以不是每天的总和,而是每小时或 5 分钟的总和。这可能吗 ?

我使用 SQL Server 2008 R2

4

1 回答 1

2

您可以使用 DATEADD 和 DATEPART 对 LogDate 进行四舍五入,然后按此分组。

示例(按五秒间隔分组):

SELECT 
  [DateLog] = DATEADD(ms,((DATEPART(ss, LogDate)/5)*5000)-(DATEPART(ss, LogDate)*1000)-DATEPART(ms, LogDate), LogDate), 
  [Sum] = COUNT(*)
FROM 
(
    SELECT LogDate = '2013-01-01 00:00:00' UNION ALL
    SELECT LogDate = '2013-01-01 00:00:04' UNION ALL
    SELECT LogDate = '2013-01-01 00:00:06' UNION ALL
    SELECT LogDate = '2013-01-01 00:00:08' UNION ALL
    SELECT LogDate = '2013-01-01 00:00:10'
) a
GROUP BY DATEADD(ms,((DATEPART(ss, LogDate)/5)*5000)-(DATEPART(ss, LogDate)*1000)-DATEPART(ms, LogDate), LogDate)
于 2013-01-17T17:59:44.280 回答