with tmp as (
select
'2023-03-21 13:30:00.000' as e_start1,
'2023-03-21 17:30:00.000' as e_end1,
'2023-03-21 14:43:36.000' as e_start2,
'2023-03-21 14:43:42.000' as e_end2
union all
select
'2023-03-21 13:30:00.000' as e_start1,
'2023-03-21 17:30:00.000' as e_end1,
'2023-03-21 14:43:49.000' as e_start2,
'2023-03-21 14:43:56.000' as e_end2
)
select
max(s) as start1,
max(e) as end1
from
(
select
(ROW_NUMBER() over (order by (select 1)) -1) / 2 as grp,
case when ROW_NUMBER() over (order by (select 1)) % 2 = 1 then tm else null end as s,
case when ROW_NUMBER() over (order by (select 1)) % 2 = 0 then tm else null end as e
from
(
select
distinct top 100 percent
case no when 1 then e_start1
when 2 then e_end1
when 3 then e_start2
when 4 then e_end2
end as tm
from tmp x
cross join (select 1 as no union all select 2 union all select 3 union all select 4) b
order by tm
) x
) y
group by grp
정보로 유추해서 쿼리를 대략 작성해봤습니다.
참고요
with tmp as (
select
'2023-03-21 13:30:00.000' as e_start1,
'2023-03-21 17:30:00.000' as e_end1,
'2023-03-21 14:43:36.000' as e_start2,
'2023-03-21 14:43:42.000' as e_end2
union all
select
'2023-03-21 13:30:00.000' as e_start1,
'2023-03-21 17:30:00.000' as e_end1,
'2023-03-21 14:43:49.000' as e_start2,
'2023-03-21 14:43:56.000' as e_end2
)
select
max(s) as start1,
max(e) as end1
from
(
select
(ROW_NUMBER() over (order by (select 1)) -1) / 2 as grp,
case when ROW_NUMBER() over (order by (select 1)) % 2 = 1 then tm else null end as s,
case when ROW_NUMBER() over (order by (select 1)) % 2 = 0 then tm else null end as e
from
(
select
distinct top 100 percent
case no when 1 then e_start1
when 2 then e_end1
when 3 then e_start2
when 4 then e_end2
end as tm
from tmp x
cross join (select 1 as no union all select 2 union all select 3 union all select 4) b
order by tm
) x
) y
group by grp