sciencebear Posted March 5, 2010 Share Posted March 5, 2010 I have a certain table, updates, with columns id, user, type, date, and time. I am trying to group them and get a count of how many in each group by user, type, and within a certain time period. However, I can't seem to get the last part to work. I can group them by user, type and day with the following query: SELECT *,COUNT(type) FROM updates GROUP BY user,type,date ORDER BY id DESC However, like I said, I need to do the same thing except narrow down the time frame. The problem is that I can't just do "GROUP BY user,type,time" because that would only do updates at the exact same second. I want to group updates that are the same user and type and about 30 minutes apart. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/194249-group-within-a-certain-time-period/ Share on other sites More sharing options...
straylight Posted March 7, 2010 Share Posted March 7, 2010 You could try something like this: SELECT *, COUNT(type) FROM updates GROUP BY user,type, GROUP BY ((60/30) * HOUR(date) + FLOOR(MINUTE(date) / 30)) ORDER BY id DESC The "30" bit can be replaced with whatever minute interval you need. Link to comment https://forums.phpfreaks.com/topic/194249-group-within-a-certain-time-period/#findComment-1022818 Share on other sites More sharing options...
straylight Posted March 7, 2010 Share Posted March 7, 2010 Oops, just realised I made a mistake in that query. It should really look like this: SELECT *, COUNT(type) FROM updates GROUP BY user,type, ((60/30) * HOUR(date) + FLOOR(MINUTE(date) / 30)) ORDER BY id DESC Link to comment https://forums.phpfreaks.com/topic/194249-group-within-a-certain-time-period/#findComment-1022825 Share on other sites More sharing options...
sciencebear Posted March 8, 2010 Author Share Posted March 8, 2010 Thanks! I had to change it a little because of the way my table was set up (date and time in separate columns) but the below code works great. Thank you very much! SELECT *, COUNT(type) FROM updates GROUP BY user,type,date, ((60/30) * HOUR(time) + FLOOR(MINUTE(time) / 30)) ORDER BY id DESC Link to comment https://forums.phpfreaks.com/topic/194249-group-within-a-certain-time-period/#findComment-1023002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.