TS SQL - 按分钟分组

时间:2009-04-16 22:04:59

标签: sql-server

我有一张带时间戳的表。获取最后一小时每分钟记录计数的正确查询是什么。

即。如果现在是2:25,我想知道有多少记录在1:25和1:26之间,1:26和1:27,依此类推,所以我有60个结​​果。

7 个答案:

答案 0 :(得分:21)

这将返回过去一小时内每分钟(您有记录的地方)的结果计数

SELECT DATEPART(n, time_stamp) AS minute, COUNT(*) as results
FROM table_name 
WHERE time_stamp > DATEADD(hh, -1, GETDATE())
GROUP BY DATEPART(n, time_stamp)

这可能会返回少于60个结果,具体取决于数据。如果您必须有60个结​​果,则查询略有不同。这使用公用表表达式生成60个数字的列表和相关的子查询,以获得每分钟的结果:

WITH numbers ( num ) AS (
    SELECT 1 UNION ALL
    SELECT 1 + num FROM numbers WHERE num < 60 )
SELECT num AS minute,
    (SELECT COUNT(*) AS results
    FROM table_name
    WHERE DATEPART(n, time_stamp) = num
    AND time_stamp > DATEADD(hh, -1, GETDATE())
FROM numbers

要查看结果,请将DATEADD(hh,-1,GETDATE())替换为DATEADD(mi,-15,GETDATE()),您将获得最近15分钟的结果,其他分钟则为0。

答案 1 :(得分:3)

编辑问题时,我编辑了答案。如果我理解正确,您只想查看过去一小时 - 即从请求发出前一小时到当前时间的时间跨度。我就是这样做的:

SELECT
    COUNT(yourTimeStamp)
FROM yourTable
WHERE DATEADD('hh', -1, GetDate()) <= yourTimeStamp 
    AND yourTimeStamp < GetDate()
GROUP BY DATEPART('mm', yourTimeStamp)

我不完全确定语法是否准确。在MSSQL中进行编码时,我会使用CURRENT_TIMESTAMP作为当前时间,MINUTE代替DATEPART等,但您可以了解解决方案。

答案 2 :(得分:2)

您正在寻找DATEPART:

declare @times table
(
    someTime datetime
)

INSERT INTO @Times (sometime) values ('jan 12 2008 12:23')
INSERT INTO @Times (sometime) values ('jan 12 2008 12:34')
INSERT INTO @Times (sometime) values ('jan 12 2008 12:35')
INSERT INTO @Times (sometime) values ('jan 12 2008 12:25')
INSERT INTO @Times (sometime) values ('jan 12 2008 12:02')
INSERT INTO @Times (sometime) values ('jan 12 2008 12:09')
INSERT INTO @Times (sometime) values ('jan 12 2008 12:35')


select DATEPART(mi,sometime) AS Minute, count(*) AS NumOccurances
from @Times
WHERE SomeTime BETWEEN @Lower AND @Upper
GROUP BY DATEPART(mi, sometime)
order by NumOccurances DESC

结果:

Minute  NumOccurances
35  2
2   1
9   1
23  1
25  1
34  1

答案 3 :(得分:1)

我发现这是一种替代方法,可用于确定每分钟插入或更新的记录数。将日期格式预先设置为变量的好处是,您可以轻松地将其更改为每小时进行分析。希望这有帮助!

DECLARE @dateFormat as varchar(max) = 'yyyy-MM-dd HH:mm'

SELECT format(timeColumn, @dateFormat) AS minute, COUNT(*) as results
FROM yourTable
WHERE timeColumn > DATEADD(hh, -1, GETDATE())
GROUP BY  format(timeColumn, @dateFormat)
ORDER BY 1

答案 4 :(得分:0)

这是我修正后的罗宾回答。我让它以正确的顺序输出错误并输出时间,而不仅仅是数字,如果你把它画出来,这不是很有用。

WITH numbers ( num ) AS (
    SELECT 1 UNION ALL
    SELECT 1 + num FROM numbers WHERE num < 60 )
SELECT (SELECT DATEADD(n, -num, GETDATE())) AS TimeStamp,
    (SELECT COUNT(*) AS results
    FROM ErrorLogs
    WHERE DATEPART(n, TimeStamp) = DATEPART(n, DATEADD(n, -num, GETDATE()))
    AND TimeStamp > DATEADD(hh, -1, GETDATE())) as Count
FROM numbers

答案 5 :(得分:0)

如果您想按分钟对结果进行分组,那么您可以使用格式化的字符串。这将按自 1900 年 1 月 1 日以来的分钟数而不是一天内的分钟分组。

WITH formatted AS (
    SELECT FORMAT(<your_datetime_column>, 'yyyy-MM-dd HH:mm') AS minute_text
    FROM <your_table>
)
SELECT minute_text, COUNT(*) AS number_of_rows
FROM formatted
GROUP BY minute_text
ORDER BY 1 DESC

答案 6 :(得分:-2)

TABLE中的SELECT COUNT(TS)TABLE.TS BETWEEN(starttime,endtime)