Because, i didn’t want to mess with the original post and use my own data stucture…
So, of course i don’t use these names, but i aim the same goal :
return a group count for every x minutes (15 is ok, i think i could change the time for my needs), AND that the results show no holes if no records : having a 0 value for those.
If you prefer i could post a new thread with the same question but with my content, i don’t mind.
But i can also give you my structure :
My table with the datetime
CREATE TABLE IF NOT EXISTS `tracking` (
`id_track` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`id_liste_track` mediumint(8) unsigned NOT NULL,
`date_track` datetime NOT NULL,
PRIMARY KEY (`id_track`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
i use your table structure for numbers
CREATE TABLE numbers ( n INTEGER NOT NULL PRIMARY KEY );
INSERT INTO numbers ( n ) VALUES
(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),... ;
AND my sample data :
INSERT INTO tracking
(id_track
, id_liste_track
, date_track
) VALUES
(1, 4, ‘2011-06-21 18:31:14’),
(2, 4, ‘2011-06-20 20:01:01’),
(3, 4, ‘2011-06-21 10:36:15’),
(4, 4, ‘2011-06-21 16:30:47’),
(5, 4, ‘2011-06-21 18:39:14’);
and the outer join request not giving me satisafation :
SELECT FROM_UNIXTIME(
CEILING(UNIX_TIMESTAMP(`date_track`)/900)*900
) AS `thedate`,
count(*) AS mycount,
`sliced`.`timeslice`
FROM (
SELECT '2011-06-20 20:01:01'
+ INTERVAL n*15 MINUTE
AS `timeslice`
FROM `numbers`
WHERE '2011-06-20 20:01:01'
+ INTERVAL n*15 MINUTE
<= '2011-06-22 19:01:01'
)
AS `sliced`
LEFT OUTER JOIN `tracking` AS NT
ON `sliced`.`timeslice` = NT.`date_track`
WHERE `sliced`.`timeslice` >= '2011-06-20 20:01:01'
AND `sliced`.`timeslice` < '2011-06-22 20:01:01'
GROUP BY `thedate`
i’m having this :
thedate | mycount | timeslice
NULL | 188 | 2011-06-20 20:16:01
2011-06-20 20:15:00 | 1 | 2011-06-20 20:01:01
But i’d like to have something like that, given the data provided :
thedate | mycount | timeslice
2011-06-20 20:15:00 | 1 | 2011-06-20 20:01:01
NULL | 0 | 2011-06-20 20:16:01
NULL | 0 | 2011-06-20 20:31:01
…
NULL | 0 | 2011-06-21 10:16:01
2011-06-21 10:36:15 | 1 | 2011-06-21 10:31:01
…
NULL | 0 | 2011-06-21 18:16:01
2011-06-21 18:31:14 | 2 | 2011-06-21 18:31:01