After breaking my head a little, I ask you to help me in the following situation:
I have a problem in SQL (Sql Server 2005) of time calculation; Basically I have to calculate the sum of hours worked on technical drives.
The issue is that a trigger can occur while the other is still open, and clutter at the time of the total sum of the person.
Ex:
- Activation 1: 06/03 12:00 --- a --- 06/03 19:00
- Activation 2: 6/03 13:00 --- a --- 06/03 15:00
- Drive 3: 06/03 20:00 --- a --- 06/03 22:00
The correct time total, in this case would be 9 hours, because the second drive does not count because it is inside the first; But if I do it by the normal SQL subtraction method, it will go wrong.
Has anyone done anything like this? Calculate the largest end date - lower starting date would also not work as we would be counting the intervals between the drives together (in the example the interval between 1 and 3).
The base generates just the date and time separately, together with the number (1st, 2nd, 3rd ...) of the drive. At most there are 7 drives.
Thank you in advance!
I made the example table to get easier:
CREATE TABLE tabela (
ID_ENTRADA INTEGER,
MATRICULA_COLABORADOR INTEGER,
DATA_AUTORIZACAO VARCHAR(50),
DATA_INICIO_HE VARCHAR(50),
HORA_INICIO_HE VARCHAR(50),
DATA_TERMINO_HE VARCHAR(50),
HORA_TERMINO_HE VARCHAR(50),
QUANTIDADE_HE VARCHAR(50),
ACIONAMENTO INTEGER
);
INSERT INTO tabela values (1, 100, '09/03/2015', '14/03/2015', '14:00:00', '14/03/2015', '16:00:00', '02:00:00', 1);
INSERT INTO tabela values (2, 100, '09/03/2015', '14/03/2015', '15:30:00', '14/03/2015', '17:00:00', '01:30:00', 2);
INSERT INTO tabela values (3, 100, '09/03/2015', '14/03/2015', '19:00:00', '14/03/2015', '22:00:00', '02:00:00', 3);
INSERT INTO tabela values (4, 100, '09/03/2015', '15/03/2015', '08:00:00', '15/03/2015', '10:00:00', '02:00:00', 1);
INSERT INTO tabela values (5, 100, '09/03/2015', '15/03/2015', '08:30:00', '15/03/2015', '10:30:00', '02:00:00', 2);
Example on SqlFiddle
In short: I want you to display the total time worked for this enrollment: 8:30:00