3

与 SQL密切相关- 从数据库中选择最“活跃”的时间跨度但不同的问题。

“我有一张交易表。在这张表中,我以 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

这样我就可以选择最“活跃”的特定年、月、日、小时、分钟和秒。

澄清一下,我不是在寻找一天中哪个小时或分钟最活跃。相反,哪个时刻最活跃。

4

2 回答 2

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
于 2012-10-17T16:34:41.517 回答
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
于 2012-10-17T16:27:31.543 回答