try see if covers all your scenario;withcte as( select *, rn = row_number() over(partition by id_call order by id_sequence) from input_table)select c1.id_call, type_call = c1.type_call, emitter_agent = c1.agent, receiver_agent = c3.agent, duration = c2.durationfrom cte c1 inner join cte c2 on c1.id_call = c2.id_call and c1.rn = c2.rn - 1 inner join cte c3 on c1.id_call = c3.id_call and c1.rn = c3.rn - 2where c1.type_call <> ''
KH[spoiler]Time is always against us[/spoiler]