I have table
CREATE TABLE card_tab (card_no NUMBER, emp_no VARCHAR2(100), DATA DATE, start_time DATE, end_time DATE) insert into card_tab (CARD_NO, EMP_NO, DATA, START_TIME, END_TIME) values (1, '100', to_date('15-11-2019', 'dd-mm-yyyy'), to_date('15-11-2019 20:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('16-11-2019 03:00:00', 'dd-mm-yyyy hh24:mi:ss')); insert into card_tab (CARD_NO, EMP_NO, DATA, START_TIME, END_TIME) values (2, '100', to_date('15-11-2019', 'dd-mm-yyyy'), to_date('15-11-2019 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('15-11-2019 23:00:00', 'dd-mm-yyyy hh24:mi:ss'));
The card_no is only sequence. Emp_no was working 7 hours
SELECT t.*, (t.end_time - t.start_time) * 24 work_hours FROM card_tab t; CARD_NO EMP_NO DATA START_TIME END_TIME WORK_HOURS 1 100 15.11.2019 15.11.2019 20:00:00 16.11.2019 03:00:00 7 2 100 15.11.2019 15.11.2019 22:00:00 15.11.2019 23:00:00 1
If hours are overlaped then sould be divided by 2 in this example it will be for
CARD_NO WORK_HOURS 1 6,5 2 0,5
The sum of working hours is 7 so it's correct. There can be more than two overlaped records. I do a lot of loops but i think this can be do more easier. It's loo like island and gap problem but i don't know how to solve it.