filter length of time [duplicate]

风流意气都作罢 提交于 2019-12-11 09:09:50

问题


I need to calculate the time that is between 8AM and 10PM. Other time I dont need. and now I do this by excel, and I want to do automate the process

I have the following table, events

start_date         |     end_date          | duration_REAL | duration_08AM_a_10PM
-------------------------------------------------------------------------------
08:00AM 20-05-2014 | 02:00 PM 20-05-2014   | 06:00         |     06:00
04:00AM 20-05-2014 | 06:00 AM 20-05-2014   | 02:00         |     00:00
08:00AM 20-05-2014 | 10:00 PM 20-05-2014   | 14:00         |     14:00
04:00AM 20-05-2014 | 11:00 PM 20-05-2014   | 19:00         |     14:00
04:00AM 20-05-2014 | 04:00 PM 21-05-2014   | 36:00         |     24:00

DATE = TIME DAY-MM-YY

What I have is the real duration, but, I want to the duration for the event between 8AM and 10PM.

For example,

  • If my event starts today at 4 AM and ends today at 5 AM in the morning, the duration of this event considering 8AM-10PM would be 00hs.
  • If my event starts today at 4 AM and ends today as 2PM, the duration of this event considering 8AM-10PM would be 6 hours.
  • If my event starts today at 2 PM and ends the following day at 2PM, the duration of 8 to 22 would be 14 hours.

回答1:


here is my answer... thanks guys!

DELIMITER $$

CREATE FUNCTION `BD`.`func_duration`(fecha_ini DATETIME, fecha_fin DATETIME)
RETURNS FLOAT 
BEGIN
DECLARE recorte FLOAT;
SET recorte = -1;

WHILE (fecha_ini <= fecha_fin) DO
    IF (HOUR(fecha_ini) >= 6 AND HOUR(fecha_ini) <= 21) THEN

        SET recorte = recorte + 1;
    END IF;
    SET fecha_ini = DATE_ADD(fecha_ini, INTERVAL 1 MINUTE);
END WHILE;
RETURN recorte;

END$$

DELIMITER ;


来源:https://stackoverflow.com/questions/23926033/filter-length-of-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!