Valid working hours

Asked

Viewed 85 times

1

How do I calculate the time spent on calls answered by an employee? However always within valid working hours, and knowing that the working hours of an employee is between 08hs and 18hs.

For example: table called.

___________________________________________________________
|cod_log|cod_chamado|inicio_log |fim_log    | status_log  |
| 001   | 100       |25/07 16:00|25/07 17:00| Atendimento |
| 002   | 100       |25/07 17:00|26/07 08:30| Pausado     |
| 003   | 100       |26/07 08:30|           | Atendimento |    
|_______|___________|___________|___________|_____________| 

As they can answer multiple calls a day, they have the possibility to leave the calls in pause until the best time to serve it, including leaving for another day.

The calculation of the time spent on answering the call I can recover, but I’m not seeing the best way to leave the calculation of breaks within working hours. In this example above cited the calculated pause of this call should be 1:30 min, that is to say it would only count until the end of your working hours, and from your work start time.

My query is like this:

SET @HoraEntrada  = '08:00:00';
SET @HoraSaida    = '20:00:00';
SELECT SEC_TO_TIME(TIMESTAMPDIFF(SECOND, q.isl_data_ini, q.isl_data_fim))
as `difference` 
FROM insuporte_status_log q 
WHERE hour(q.isl_data_ini) >= @HoraEntrada and hour(q.isl_data_fim) <= @HoraSaida
and  q.isl_sch_codigo = '100405-573'
and (q.isl_status = 'Pausado')

And yet you keep counting the time that theoretically should be out of range.

What should my approach be like in this case? Any suggestions?

  • "leave the calculation within working hours" the valid interval (working time), has to be filtered in the where

  • Do you have the schedule of the preview in your table? Could you guess the structure of the table of appointments and some examples of content?

  • The hours of attendance is that set above 8hs to 18hs, my question is whether for example I "pause" an appointment today at 17hs and resume tomorrow at 8hs, count only 1 hour of break, and not 15 as occurs today.

  • I edited again now with some fictitious table data for a better understanding of the problem.

1 answer

1

You can use the function [IF()][1] so that when the time is less than @HoraEntrada you switch to own @HoraEntrada and if it’s bigger than @HoraSaida also assume the @HoraSaida, would look something like this:

SET @HoraEntrada  = '08:00:00';
SET @HoraSaida    = '20:00:00';
SELECT SEC_TO_TIME(
    TIMESTAMPDIFF(
        SECOND,
        IF(q.isl_data_ini < HoraEntrada,
            @HoraEntrada,
            IF(q.isl_data_ini > @HoraSaida,
                @HoraSaida,
                q.isl_data_ini
            )
        ),
        IF(q.isl_data_ini < HoraEntrada,
            @HoraEntrada,
            IF(q.isl_data_fim > @HoraSaida,
                @HoraSaida,
                q.isl_data_fim
            )
        )
    )
) as `difference` 
FROM insuporte_status_log q 
WHERE q.isl_sch_codigo = '100405-573'
and (q.isl_status = 'Pausado')
  • Hello, thanks for the help, I adapted and used the cited example, but even so the total time count paused continues counting the time between the "@Horasaida" and "@Horaentrada", this time should not be taken into account.

  • I can not test, put a structure so that we can perform some tests in http:/sqlfiddle.com/

  • The total pause should be 4hs and for this structure the difference is 16hs. http://sqlfiddle.com/#! 9/417be9/1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.