This is a complex one. But I have a table which has a DATETIME field, and a few other int and float fields which need to be summed and averaged. We want to do the summing and averaging on this table based on the time stamps and ultimately would aim to develop 3 queries which in a sense would build on one another.
So the able looks like this
TIMESTAMP |subj_diff| SCR2 | SCR3
2011-09-20 09:01:37 | 1 | 0.02 | 1.6
2011-09-20 09:04:18 | 3 | 0.09 | 1.8
2011-09-20 14:24:55 | 5 | 0.21 | 1.2
2011-09-21 18:50:47 | 8 | 0.08 | 0.9
2011-09-21 18:54:21 | 9 | 0.12 | 2.1
The three queries that we would like to generate are:
1. Sum up all the preceding items from a previous data up to and including the currently select record. There should also be another column with the total So say for example if we wanted the results between the 20th and 21st the returned table would look like:
TIMESTAMP |subj_diff| SCR2 | SCR3 | COUNT
2011-09-20 09:01:37 | 1 | 0.02 | ... | 1
2011-09-20 09:04:18 | 4 | 0.11 | | 2
2011-09-20 14:24:55 | 9 | 0.32 | | 3
2011-09-21 18:50:47 | 17 | ...
2011-09-21 18:54:21 | 26 |
2. Sum up the results at 5 minute time intervals - similar to the above however the query would return 3 rows as rows 1 & 2 and rows 4&5 would be summed together in the same fashion as above. IN this query its ok if for each 5 min interval that has nothing 0 is returned with a count of 0. E.g.
TIMESTAMP |subj_diff| SCR2 | SCR3 | COUNT
2011-09-20 09:05:00 | 4 | 0.11 | 3.4 | 2
2011-09-20 14:25:00 | 5 | 0.21 | 1.2 | 1
2011-09-21 18:55:00 | 17 | 0.20 | 3.0 | 2
3. Do the same thing in query number 1 for the result set of query number two for every 5 minute interval in the day (i.e. from 00:05:00 to 24:00:00).
This is a rather tricky one, I have no idea how to start this one. Would anyone be able to write SQL to solve this problem?
Heres some basic code using cursors and stored procs but it doesnt really work.
DROP PROCEDURE curdemo;
DELIMITER $$
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a datetime;
DECLARE b,c FLOAT;
DECLARE cur1 CURSOR FOR
SELECT msgDate, subj_diff FROM classifier_results
WHERE DATE(msgDate) >= DATE('2011-09-25');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_scores (d datetime, acc float);
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a, b;
IF done THEN
LEAVE read_loop;
END IF;
INSERT temp_scores(d,acc)
SELECT a, SUM(subj_diff) FROM classifier_results
WHERE DATE(msgDate) >= DATE('2011-09-25')
AND msgDate <= a;
END LOOP;
CLOSE cur1;
SELECT * FROM temp_scores;
END;
Cheers!
truncate(hour / 12)
as well as the day and hour then do your additional averages and counts... – Ben Sep 25 '11 at 22:18