у меня есть эта рабочая процедура mysql, есть ли способ сделать это для цикла for для проверки времени, чтобы минимизировать код
и это код процедуры, который я хочу усреднить за 2 часа в течение одного дня, день определяется параметром daydate
и время от «00:00:00» до «24:00:00»
CREATE DEFINER=`user`@`localhost` PROCEDURE `proceduregetavgperday`(IN daydate date)
BEGIN
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"00:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '00:00:00' and `Time` < '02:00:00' and `Date`=daydate)
union
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"02:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '02:00:00' and `Time` < '04:00:00' and `Date`=daydate)
union
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"04:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '04:00:00' and `Time` < '06:00:00' and `Date`=daydate)
union
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"06:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '06:00:00' and `Time` < '08:00:00' and `Date`=daydate)
union
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"08:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '08:00:00' and `Time` < '10:00:00' and `Date`=daydate)
union
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"10:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '10:00:00' and `Time` < '12:00:00' and `Date`=daydate)
union
(select IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Happiness`), 2),0) AS `Happiness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Anger`), 2),0) AS `Anger`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Contempt`), 2),0) AS `Contempt`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Disgust`), 2),0) AS `Disgust`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Fear`), 2),0) AS `Fear`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Neutral`), 2),0) AS `Neutral`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Sadness`), 2),0) AS `Sadness`,
IFNULL(ROUND(AVG(`viewemotionavgbydateandtime`.`Surprise`), 2),0) AS `Surprise`,
"12:00:00" as "Hour"
from viewemotionavgbydateandtime where `Time` >= '12:00:00' and `Time` < '14:00:00' and `Date`=daydate)
;
END