SQL - 选择最活跃的'来自db的时间

时间:2012-10-17 16:24:33

标签: sql tsql datetime stored-procedures

SQL - Select most 'active' timespan fromdb非常密切相关,但问题不同。

“我有一个交易表。在此表中,我将交易日期时间存储在UTC中。我有几个月的数据,每天约20,000笔交易。”

如何改变

  select datepart(hour, the_column) as [hour], count(*) as total 
  from t 
  group by datepart(hour, the_column) 
  order by total desc

这样我就可以选择最活跃的特定年份,月份,日期,时间,分钟和秒。

为了澄清,我不是在寻找当天哪个小时或哪个时段最活跃。相反,哪个时刻最活跃。

2 个答案:

答案 0 :(得分:2)

Select 
    DATEPART(year, the_column) as year
    ,DATEPART(dayofyear,the_column) as day
    ,DATEPART(hh, the_column) as hour
    ,DATEPART(mi,the_column) as minute
    ,DATEPART(ss, the_column) as second
    ,count(*) as count from t
Group By 
    DATEPART(year, the_column)
    , DATEPART(dayofyear,the_column)    
    , DATEPART(hh, the_column)
    , DATEPART(mi,the_column)
    , DATEPART(ss, the_column)
order by count desc

答案 1 :(得分:2)

如果分钟分辨率足够:

select top 1 cast(the_column as smalldatetime) as moment, count(*) as total 
from t 
group by cast(the_column as smalldatetime)
order by total desc