# Grouping data by 15 minute intervals

After a rather long course change (I was doing little work with SQL Server and work with other RDBMS systems), I'm back working on Microsoft SQL Server and a few other Microsoft products. Here is an interesting problem presented to me a few days ago.

A colleague of mine need timed data grouped by 15 minute intervals. I found this to be an interesting problem and didn't see much online, so thought I'd post this quick tidbit for others to find. The technique can be used to group by any interval with a little tweaking.

The field in question is a datetime field. The code needed to group data in 15 min intervals is this:

datepart(mi,C001_TimeEnd)/15)*15

Let's break it down. I take the minutes of the datetime field and divide it by 15, which is my interval. That will give me a number between 0 and 3, because it is an integer/integer. I then multiple that by 15. The distribution is as follows:

• (00-14)/15 = 0*15=0
• (15-29)/15= 1*15 = 15
• (30-44)/15=2*15=30
• (45-59)=3*15=45

Datepart(mi,"2018-5-9 11:48:33")/15) = 48/15 = 3*15 = 45. Now add in the the date and hours.

left(convert(varchar,"2018-5-9 11:48:33",120),14)+RIGHT('00' + CONVERT(VARCHAR(2), (datepart(mi,"2018-5-9 11:48:33")/15)*15), 2) = "2018-5-9 11:45:00"

If you wanted to do a 10 min interval, this: datepart(mi,C001_TimeEnd)/15)*15 becomes this datepart(mi,C001_TimeEnd)/10)*10.