A couple weeks ago I hacked up some sql for this and had planned to blog about it but never got around to it. Of course then I needed to use the technique again yesterday, so it’s time to make a more permanent record for next time. The problem we’re solving is the need to do group by on time intervals of less than 1 hour. This isn’t exactly hard, but it’s a little tricky, since you can’t easily pull out random fractions of an hour. The way I solved it was working my way through a series of sql statements.This first sql statement has two parts; first I grab the current time and use the date_trunc function to cut it to the current minute. This isn’t needed for the final query, it is more for reference while I worked things out. The second part of the query I used the extract function to get just the minute portion of the time, in this case 47 minutes. Next, I divided the minutes by 15, to give me the quarter hour. Of course the decimal is a bit unwieldy, so… I used the round function to get something a little nicer to work with. Now, this tells us our timestamp is in the third quarter hour, but if we’re going to have something to group by on, we want something a little more real world. So now we have our time, rounded to the quarter hour of 45 minutes, and the only thing left to do is turn it back into a normal timestamp. Note here I used date_trunc to get my time to set to the base time of the hour, and then I add back in the ‘45 minutes’ that are generated through our series of rounding and extracting. Once you have this, using it in an aggregate is only a short step away. Note you can adjust the interval (eg. make it every 5 minutes) by changing the number you divde and multiply by, so this technique should work for any sub-hour intervals, and would not be hard to adapt to other non-standard amounts (like every 4 hours). Also remember this was something I whipped out rather quickly, I’ve done no analysis on how well it scales, if you’ve worked out a better/cleaner query by all means, please post to the comments. HTH.