I have an table in Oracle
| shift_id | timestamp_(oracle) | type_ |
|---|---|---|
| 00000001 | 17/05/2022 08:00 | 0001 |
| 00000001 | 17/05/2022 09:00 | 0002 |
| 00000001 | 17/05/2022 09:15 | 0003 |
| 00000001 | 17/05/2022 12:00 | 0002 |
| 00000001 | 17/05/2022 13:00 | 0003 |
| 00000001 | 17/05/2022 15:00 | 0004 |
type 1 is starttime, type 2 is pause, type 3 is continue after pause, type 4 is end_time
I want to calculate the effective shift time, one way to do this in my opinion is to calculate the sum of all start and end times and substract them, for example:
(sum(type 0002) + sum (type 0004)) - (sum (type 0001) + sum (type 0003))
But how to do this in oracle sql?
I've tried: select shift_id, sum(to_number_to_char(timestamp_,'sssss') where (type_ = 0002 or type is 0004) group by shift_id
Then I get a result like 00000001, 82442 But the sum is not the right sum, it only calculates with seconds after midnight.
Question: How to get a result like: 00000001, 05:45