2014-04-21 77 views
0

我有一個表格,其中有1小時內發生的事件。以5秒的時間間隔查詢組數據

2014-04-16 13:56:06.971 , 3474 
2014-04-16 13:56:07.061 , 3609 
2014-04-16 13:56:07.067 , 3617 

表有時間戳和事件ID

我想組的數據有5秒的時間間隔發生的事件數的計數,所以它看起來像這樣:

0-5 sec., 3 
5-10 sec. , 6 
10-15 sec. , 4 

在此先感謝! 我使用SQL Server 2008的

回答

0

您可以使用這樣的查詢:

declare @Hour datetime = '2014-04-16 13:00:00' -- starting time 

select 
    CONVERT(VARCHAR(10), (DATEDIFF(second, @Hour, EventDateTime)/5) * 5) 
    + '-' + CONVERT(VARCHAR(10), (DATEDIFF(second, @Hour, EventDateTime)/5 + 1) * 5) + ' sec', 
    COUNT(EventId) 
from intervals 
group by DATEDIFF(second, @Hour, EventDateTime)/5 
0

如何這樣的事情,在這裏你按秒/ 12

select 
    convert(varchar(5),[time stamp],108) as [hh:mm], 
    cast(min(datepart(s, [time stamp])) as varchar(10)) + ' - ' + cast(max(datepart(s, [time stamp])) as varchar(10)) as [second interval], 
    count(*) as total 
from yourtable 
group by convert(varchar(5),[time stamp],108), (datepart(s, [time stamp])/12) 

或者你可以用開始/結束秒創建一個理貨表,然後加入到它。