Вы все еще можете написать рекурсивный CTE, используя эти таблицы. Однако кодирование становится сложным.
Вот CTE. Возможно, вам придется настроить его, чтобы получить именно то, что вы хотите. Я изменил имена столбцов для простоты:
with
c as (
select 'v' as type, vid as id, contact, 0 as lvl, cast(concat('/', vid, '/') as varchar(255)) as path
from (select *, row_number() over(partition by vid order by contact) as rn from v) x where rn = 1
union all
select
case when type = 'v' then 'e' else 'v' end, -- type
case when type = 'v' then v.eid else v.vid end, -- id
v.contact,
c.lvl + 1,
cast(concat(c.path, case when type = 'v' then v.eid else v.vid end, '/') as varchar(255))
from c
join v on c.lvl <= 10 and v.contact >= c.contact and (c.type = 'v' and v.vid = c.id or c.type = 'e' and v.eid = c.id)
and c.path not like concat('%', case when type = 'v' then v.eid else v.vid end, '%')
)
select * from c order by path
Результат:
type id contact lvl path
---- ---- --------------------- --- -----------------------
v 100 2020-03-11 01:00:00.0 0 /100/
e X123 2020-03-11 01:00:00.0 1 /100/X123/
v 101 2020-03-12 01:11:00.0 2 /100/X123/101/
v 103 2020-03-12 11:00:00.0 2 /100/X123/103/
e X124 2020-03-11 01:10:00.0 1 /100/X124/
v 102 2020-03-12 10:00:00.0 2 /100/X124/102/
e X126 2020-03-12 10:00:00.0 3 /100/X124/102/X126/
v 104 2020-03-12 12:00:00.0 4 /100/X124/102/X126/104/
v 105 2020-03-12 12:00:00.0 4 /100/X124/102/X126/105/
v 104 2020-03-12 12:00:00.0 2 /100/X124/104/
e X126 2020-03-12 12:00:00.0 3 /100/X124/104/X126/
v 105 2020-03-12 12:00:00.0 4 /100/X124/104/X126/105/
v 101 2020-03-11 01:20:00.0 0 /101/
e X123 2020-03-12 01:11:00.0 1 /101/X123/
v 103 2020-03-12 11:00:00.0 2 /101/X123/103/
e X125 2020-03-11 01:20:00.0 1 /101/X125/
v 102 2020-03-12 10:00:00.0 0 /102/
e X124 2020-03-12 10:00:00.0 1 /102/X124/
v 104 2020-03-12 12:00:00.0 2 /102/X124/104/
e X126 2020-03-12 12:00:00.0 3 /102/X124/104/X126/
v 105 2020-03-12 12:00:00.0 4 /102/X124/104/X126/105/
e X126 2020-03-12 10:00:00.0 1 /102/X126/
v 104 2020-03-12 12:00:00.0 2 /102/X126/104/
e X124 2020-03-12 12:00:00.0 3 /102/X126/104/X124/
v 105 2020-03-12 12:00:00.0 2 /102/X126/105/
v 103 2020-03-12 11:00:00.0 0 /103/
e X123 2020-03-12 11:00:00.0 1 /103/X123/
v 104 2020-03-12 12:00:00.0 0 /104/
e X124 2020-03-12 12:00:00.0 1 /104/X124/
e X126 2020-03-12 12:00:00.0 1 /104/X126/
v 105 2020-03-12 12:00:00.0 2 /104/X126/105/
v 105 2020-03-12 12:00:00.0 0 /105/
e X126 2020-03-12 12:00:00.0 1 /105/X126/
v 104 2020-03-12 12:00:00.0 2 /105/X126/104/
e X124 2020-03-12 12:00:00.0 3 /105/X126/104/X124/
Для справки, вот скрипт данных, который я использовал для тестирования, если вам нужно создать SQL Fiddle запустить его:
create table v (
vid varchar(6),
eid varchar(6),
contact datetime
);
insert into v (vid, eid, contact) values ('100', 'X123', '2020-03-11 01:00:00');
insert into v (vid, eid, contact) values ('100', 'X124', '2020-03-11 01:10:00');
insert into v (vid, eid, contact) values ('101', 'X123', '2020-03-12 01:11:00');
insert into v (vid, eid, contact) values ('101', 'X125', '2020-03-11 01:20:00');
insert into v (vid, eid, contact) values ('102', 'X126', '2020-03-12 10:00:00');
insert into v (vid, eid, contact) values ('102', 'X124', '2020-03-12 10:00:00');
insert into v (vid, eid, contact) values ('103', 'X123', '2020-03-12 11:00:00');
insert into v (vid, eid, contact) values ('104', 'X124', '2020-03-12 12:00:00');
insert into v (vid, eid, contact) values ('104', 'X126', '2020-03-12 12:00:00');
insert into v (vid, eid, contact) values ('105', 'X126', '2020-03-12 12:00:00');