NQL的TSQL集团

时间:2011-04-01 12:11:30

标签: tsql

我可以按秒或分钟进行分组:

SELECT datepart(minute, Time)
      ,count(*) as hits
FROM Log
GROUP BY datepart(minute, Time)

有没有办法可以做同样的事情,但是有指定的秒数,所以按“每10秒”分组一次?

更多信息:
这与之间的时间相结合:

SELECT datepart(minute, Time)
      ,count(*) as hits
FROM Log with (nolock)
WHERE Time between dateadd(minute, -2, getdate()) and getdate()
GROUP BY datepart(minute, Time)

1 个答案:

答案 0 :(得分:5)

试试这个:

DATEDIFF (ss ,'19700101' ,Time ))将给出自1970年1月1日以来的秒数(您可以选择小于所有日期的其他日期)。除以10,每10秒钟就有一个GROUP BY:

SELECT DATEDIFF (ss ,'19700101' ,Time )/10, count(*) as hits
  FROM Log
 GROUP BY DATEDIFF (ss ,'19700101' ,Time )/10

(随着时间的推移):

SELECT convert(varchar, min(Time), 108) time,
       DATEDIFF (ss ,'19700101' ,Time )/10 sec_interval,
       count(*) as hits
  FROM Log
 GROUP BY DATEDIFF (ss ,'19700101' ,Time )/10