2
I have the following problem I have the data that way:
codigo data_hora nome_ajudante
-------+----------------+--------------
1000004 2018-08-22 11:11 Carlos Eduardo
1000004 2018-08-22 11:43 Carlos Eduardo
1000004 2018-08-22 11:48 Carlos Eduardo
1000004 2018-08-22 11:54 Carlos Eduardo
1000004 2018-08-22 17:52 Carlos Eduardo
1000004 2018-08-23 08:13 Carlos Eduardo
1000004 2018-08-23 08:28 Carlos Eduardo
1000004 2018-08-23 10:25 Carlos Eduardo
1000004 2018-08-23 10:25 Carlos Eduardo
1000004 2018-08-23 10:25 Carlos Eduardo
1000004 2018-08-23 13:30 Carlos Eduardo
1000004 2018-08-24 22:20 Carlos Eduardo
1000004 2018-08-24 23:27 Carlos Eduardo
1000004 2018-08-25 03:14 Carlos Eduardo
1000004 2018-08-25 05:12 Carlos Eduardo
I would need to take the first and last time of the day, but also when break is night, so:
codigo inicio fim nome_ajudante
-------+-------------------+-------------------+--------------
1000004 2018-08-22 11:11:00 2018-08-22 17:52:00 Carlos Eduardo
1000004 2018-08-23 08:13:00 2018-08-23 13:30:00 Carlos Eduardo
1000004 2018-08-24 22:20:00 2018-08-25 05:12:00 Carlos Eduardo
I used this query, but days 24 and 25 divided, due to the grouping (GROUP BY).
SELECT
MAX(codigo) as codigo
, MIN(data_hora) AS inicio
, MAX(data_hora) AS fim
, nome_ajudante
FROM
diarias_ajudantes.registro_terceiros
GROUP BY nome_ajudante, EXTRACT(DAY FROM data_hora), EXTRACT(MONTH FROM data_hora), EXTRACT(YEAR FROM data_hora)
ORDER BY nome_ajudante, inicio ASC
Does anyone know a way to achieve the expected result?
My comrade, thank you. For the above approach was correct, but for times less than 8:00 did not work, as you mentioned before. For the larger sample I have, I changed the interval '8' hour for 6 hours. But in the universe that I will work, there will be time 5h in the morning approximately that will be the beginning of the journey and 7h in the morning that will be the end of the journey. If I had the day you meant it would be quiet, but I won’t have that information.
– thiagofred
Really hard there. You’d need an extra field in the table, like
data_hora_saida
, or a flag indicating whether it is input or output. Another way out, half-way rs, would be to record some more information in the current fields, such as adding a'*'
in the helper name indicating the output, or writing positive code for input and negative for output (1000004
is entrance,-1000004
indicates output from the same code).– Dudaskank
In fact, we will have a device where the person will make interactions, then would need to calculate the time between the first and the last interaction. But people would not have fixed hours for such interaction. Therefore, I have no flag option for this.
– thiagofred
These start and end times, do you have them (or could you have) registered for each employee somewhere in the BD? If you have, you could possibly use to subtract these values from the field
data_hora
– Dudaskank
If I have a preset shift for each employee on another schedule, would that be possible? For example, the normal shift of this employee would be 08:00 to 18:00. Delays and HE will not be treated here. Would this data help? I thought something might be possible, but I’m not getting it.
– thiagofred
Yes, I did an update on the reply to take into account this other table.
– Dudaskank
Understood, but if there is an incidence 5 minutes before, this data will be like the previous day.
– thiagofred
Then in the case, you could add a few minutes in group by as well, but the same would be worth for the departure time. 100% solution I think you won’t have, only if you could include more information even in the tegistro table, or even in another that could complement.
– Dudaskank
Thank you for your time, but for now my situation is complicated. I wish I had more information to speed this up, but the worst thing is that I don’t have rs
– thiagofred