5

I have a table that looks like this

CREATE TABLE `time_table` (
 `id` INT(10) NOT NULL AUTO_INCREMENT,
 `creationDate` DATETIME NOT NULL,
 PRIMARY KEY (`id`)
)

I basically store the creation time of certain records in the table. I know if I want to get a count of the records that were created in 15 mins interval I will use something like this

SELECT FLOOR(UNIX_TIMESTAMP(creationDate)/900) AS t, 
COUNT(*) FROM time_table
GROUP BY t

That gives me something like this

t          COUNT(*)
1434187    1
1434188    3
1434189    2
1434190    2

How do I make sense of the first column? If I want it to show me something like

t                 COUNT(*)
2:00pm - 2:15pm   1
2:15pm - 2:30pm   3
2:30pm - 2:45pm   2
2:45pm - 3:00pm   2

I understand that with some manipulation I could get 1434187 to show up at 2:15pm. Even that might be a good start....then with some logic I could show the entire period. Thanks!

Girish Dusane
  • 1,120
  • 4
  • 12
  • 19

3 Answers3

9

Here is the solution I use:

SELECT  FROM_UNIXTIME(FLOOR( UNIX_TIMESTAMP(creationDate)/900 ) * 900) AS t, COUNT(*) FROM time_table GROUP BY t

Although it arrives 4 years later, hope it will be useful to someone.

Prasad DLV
  • 408
  • 4
  • 9
Chebe
  • 91
  • 1
  • 2
7

One way is to just use > and < in order to get everything within a range, but you may find this to be simpler:

SELECT ... 

GROUP BY ( 4 * HOUR( thistime ) + FLOOR( MINUTE( thistime ) / 15 )) 

from http://forums.mysql.com/read.php?10,202789,202807

James Black
  • 41,583
  • 10
  • 86
  • 166
  • 1
    +1 for the simple and clean solution, but something goes wrong here, the count is not correct... (in the select...) and it works only for 24 hours... – Dani Feb 25 '12 at 06:31
  • @Dani - What does your 'where' clause look like? – James Black Feb 26 '12 at 14:18
5

Answer 2 only works for 24hours - try this one;

SELECT ...

GROUP BY year(date),month(date),day(date),( 4 * HOUR( date ) + FLOOR( MINUTE( date ) / 15 )) 
PodTech.io
  • 4,874
  • 41
  • 24