the base query works like intenden, but when I try to sum the first columns, its value should be 5, but insted I get 4, why?
basic request:
SET @last_task = 0; SELECT IF(@last_task = RobotShortestPath, 0, 1) AS new_task, @last_task := RobotShortestPath FROM rob_log ORDER BY rog_log_id; 1 1456 0 1456 0 1456 1 1234 0 1234 1 1456 1 2556 1 1456
amount request
SET @last_task = 0; SELECT SUM(new_task) AS tasks_performed FROM ( SELECT IF(@last_task = RobotShortestPath, 0, 1) AS new_task, @last_task := RobotShortestPath FROM rob_log ORDER BY rog_log_id ) AS tmp 4
table structure
CREATE TABLE rob_log ( rog_log_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, # RobotPosX FLOAT NOT NULL, # RobotPosY FLOAT NOT NULL, # RobotPosDir TINYINT UNSIGNED NOT NULL, RobotShortestPath MEDIUMINT UNSIGNED NOT NULL, PRIMARY KEY(rog_log_id), KEY (rog_log_id, RobotShortestPath) ); INSERT INTO rob_log(RobotShortestPath) SELECT 1456; INSERT INTO rob_log(RobotShortestPath) SELECT 1456; INSERT INTO rob_log(RobotShortestPath) SELECT 1456; INSERT INTO rob_log(RobotShortestPath) SELECT 1234; INSERT INTO rob_log(RobotShortestPath) SELECT 1234; INSERT INTO rob_log(RobotShortestPath) SELECT 1456; INSERT INTO rob_log(RobotShortestPath) SELECT 2556; INSERT INTO rob_log(RobotShortestPath) SELECT 1456;
testing on sqlfiddle: http://sqlfiddle.com/#!2/e80f5/3 as an answer. Counting changes in history with MySQL but confusedly confused
source share