For each row, find the next closest value in another table (Teradata)
select CHAS_INIT, CHAS_NR, Ingate_TIME,
   -- find the previous IN-time
   last_value(case when inout = 'i' then Ingate_TIME end ignore nulls)
   over (partition by CHAS_INIT, CHAS_NR -- does this determine a truck?
         order by Ingate_TIME)
from
 (  -- combine both in &amp; out into a single result
    select 'i' as inout, CHAS_INIT, CHAS_NR, Ingate_TIME
    from session.Ingate
    union all 
    select 'o' as inout, CHAS_INIT, CHAS_NR, Ingate_TIME
    from session.Outgate
 ) as dt
-- only return OUT rows
qualify inout = 'o'