sql - Forming query in DB2 to fetch row based on the values in one column along with order of another column -


i apologize if title seems absurd , lack information trying explain situation through following example:

consider following table-

id     event     time --------------------- 1      eventa     ta 1      eventb     tx 2      eventb     ty 1      eventc     tb 2      eventc     

i wish select id such there eventc after(based on time) instance of eventb.

i think of following query:

 select id tabet     ((select time tabet event = eventc order time desc fetch first row only)     >      (select time tabet event = eventb order time desc fetch first row only)) 

i looking better approach , alternative table in reality big table , query subquery inside big query satisfy condition.

edit

the id not unique. problem identify ids whcich there eventc after(based on time) eventb

you can use self join:

select distinct t1.id     table t1    join table t2 on        t1.id = t2.id ,       t1.event = 'eventb' ,       t2.event = 'eventc' ,       t2.time > t1.time 

another approach:

with latest_times (    select id, max(time) time table         event='eventc'        group id ) select t1.id table t1     join latest_times on          t1.id = latest_times.id ,         t1.event = 'eventb' ,         latest_times.time > t1.time  

Comments