Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - select most 'active' time from db

Very closely related to SQL - Select most 'active' timespan fromdb but different question.

"I have a table of transactions. In this table I store the transaction datetime in UTC. I have a few months of data, about 20,000 transactions a day."

How would change

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

so that I can select the specific year, month, day, hour, minute, and second that was the most 'active'.

To clarify, I'm not looking for which hour or minute of the day was most active. Rather, which moment in time was the most active.

like image 460
Mark Avatar asked May 14 '26 08:05

Mark


1 Answers

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
like image 157
Tarynn Avatar answered May 16 '26 21:05

Tarynn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!