When retrieving data for statistical usage in SQL it´s often useful to group the data to some kind of time interval. Easiest and most used is probably simple grouping by date:

SELECT cast(Timestamp as date), COUNT(*) FROM MyTable GROUP BY cast(Timestamp as date)

However, some times you will want to go even deeper in detail and group things based on hours or maybe even minutes. This can be achieved by rounding the timestamp. What we do is simply to count the hours/minutes from zero-time and add this to a zero-time date. Beginning-Of-Time + COUNT(Hours from Beginning-Of-Time) = Your datetime rounded to hours

SELECT dateadd(HOUR, datediff(HOUR, 0, Timestamp), 0), COUNT(*) FROM MyTable GROUP BY dateadd(HOUR, datediff(HOUR, 0, Timestamp), 0)
Why not join the newsletter?

Why not join the newsletter?

If you're not a fan of facebook or twitter, sign up to the newsletter and I'll send you a quick update every so often with what's going on and new posts coming out.

You have Successfully Subscribed!