Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Oracle 11g - SQL zum Berechnen des Zeitunterschieds zwischen mehreren Zeilen

Hinweise .... Sie sollten keine redundanten Daten (Vorname, Nachname) in dieser Tabelle haben, Sie sollten nur dafür eine separate Tabelle haben. Anscheinend sind Ihre Stunden abgeschnitten und nicht gerundet? (Die Rundung würde 1,26 in der ersten Zeile ergeben).

with
     test_data ( seq, the_date, scanid, locatn, user_id, first_name, last_name ) as (
       select 103939758, to_date('05-NOV-16 14:36:22', 'dd-MON-yy hh24:mi:ss'), 194972, 'DOOR 19', 'AX9868', 'Mike', 'Derry'  from dual union all
       select 103939780, to_date('05-NOV-16 14:38:07', 'dd-MON-yy hh24:mi:ss'), 194972, 'DOOR 19', 'AX9868', 'Mike', 'Derry'  from dual union all
       select 103939792, to_date('05-NOV-16 14:39:24', 'dd-MON-yy hh24:mi:ss'), 194972, 'DOOR 19', 'AX9868', 'Mike', 'Derry'  from dual union all
       select 103940184, to_date('05-NOV-16 15:16:53', 'dd-MON-yy hh24:mi:ss'), 194972, 'DOOR 19', 'AX9868', 'Mike', 'Derry'  from dual union all
       select 103940185, to_date('05-NOV-16 15:51:41', 'dd-MON-yy hh24:mi:ss'), 194972, 'DOOR 19', 'AX9868', 'Mike', 'Derry'  from dual union all
       select 103940214, to_date('05-NOV-16 09:51:42', 'dd-MON-yy hh24:mi:ss'), 194993, 'DOOR 16', 'BC1910', 'Tony', 'McCann' from dual union all
       select 103940215, to_date('05-NOV-16 15:19:06', 'dd-MON-yy hh24:mi:ss'), 194993, 'DOOR 16', 'BC1910', 'Tony', 'McCann' from dual
     )
-- end of test data; solution (SQL query) begins below this line
select trunc(the_date) as the_date, user_id, first_name, last_name,
       trunc(24 * (max(the_date) - min(the_date)), 2) as total_hrs
from   test_data
group by trunc(the_date), user_id, first_name, last_name
;

THE_DATE  USER_ID FIRST_NAME LAST_NAME  TOTAL_HRS
--------- ------- ---------- --------- ----------
05-NOV-16 AX9868  Mike       Derry           1.25
05-NOV-16 BC1910  Tony       McCann          5.45