ORACLE SQL Joining tables based on different time based events -
ORACLE SQL Joining tables based on different time based events -
i have 2 tables:
processor_event(id, time, machine, name, state) comm_event(id, time, machine, name, type)i want retrieve difference between processor_event.time , next comm_event in time before next processor_event.time.
i.e. first comm_event event after processor_event occurs before next processor_event.
example data:
processor_event(id, time, machine, name, state) 1, 01:00:00, trx4, ignition, heated 2, 01:00:03, trx1, movement, triangulating 3, 01:00:23, trx4, movement, heated 4, 01:00:32, trx4, direction change, stable 5, 01:00:56, trx4, stopping, heated comm_event(id, time, machine, name, type) 1, 01:00:02, trx4, direction request, request 2, 01:00:15, trx4, direction acknowledgement, acknowledgement 3, 01:00:16, trx1, position change, command 4, 01:00:34, trx4, direction request, request 5, 01:01:02, trx4, position change, command
expected return:
processor_event.time, processor_event.machine, processor_event.name, comm_event.time, comm_event.type, timedifference 01:00:00, trx4, ignition, 01:00:02, request, 2 01:00:03, trx1, movement, 01:00:16, command, 13 01:00:23, trx4, movement, null, null, null 01:00:32, trx4, direction change, 01:00:34, request, 11 01:00:56, trx4, stopping, 01:01:02, command, 6
any ideas on how go doing great, in advance time.
try query:
select to_char(t.time, 'hh24:mi:ss') "processor_event.time", t.machine "processor_event.machine", t.name "processor_event.name", to_char(t.ce_time, 'hh24:mi:ss') "comm_event.time", t.ce_type "comm_event.type", (t.ce_time - t.time)*24*60*60 timedifference ( select pe.*, row_number() on (partition pe.id order ce.time) rn, ce.time ce_time, ce.type ce_type ( select processor_event.*, lead(time) on (partition machine order time) next_pe_time processor_event -- here set filtering conditions processor_event table ) pe left outer bring together comm_event ce on ce.machine = pe.machine , ce.time > pe.time , (pe.next_pe_time null or ce.time < pe.next_pe_time) ) t t.rn = 1 order t.time
test on sql fiddle
sql oracle
Comments
Post a Comment