以及它不是真正的“好”看,我会尽力,明天找到一个更好的解决方案。
create table tt (id int, dte_in timestamp, dte_out timestamp);
insert into tt values (1, timestamp('2016-10-10-06.58.01'), timestamp('2016-10-10-14.59.01'));
insert into tt values (1, timestamp('2016-10-11 06:57:02'), timestamp('2016-10-11 14:58:23'));
insert into tt values (1, timestamp('2016-10-12 06:57:02'), timestamp('2016-10-12 14:58:23'));
insert into tt values (1, timestamp('2016-10-13 06:57:02'), timestamp('2016-10-13 14:58:23'));
insert into tt values (1, timestamp('2016-10-14 06:57:02'), timestamp('2016-10-14 14:58:23'));
insert into tt values (2, timestamp('2016-10-10 07:51:01'), timestamp('2016-10-10 13:00:01'));
insert into tt values (2, timestamp('2016-10-12 05:57:02'), timestamp('2016-10-12 15:58:23'));
insert into tt values (2, timestamp('2016-10-13 05:57:02'), timestamp('2016-10-13 12:58:23'));
insert into tt values (2, timestamp('2016-10-14 10:57:02'), timestamp('2016-10-14 16:58:23'));
select a.id,
a.dte_in,
b.dte_in
from tt a
inner join tt b on b.id = 2 and b.dte_in between a.dte_in and a.dte_out
where a.id = 1
union all
select a.id,
b.dte_out,
a.dte_out
from tt a
inner join tt b on b.id = 2 and b.dte_out between a.dte_in and a.dte_out
where a.id = 1
union all
select a.id, a.dte_in, a.dte_out
from tt a
where a.id = 1 and not exists (select 1 from tt b where b.id = 2 and b.dte_in between a.dte_in and a.dte_out)
and not exists (select 1 from tt b where b.id = 2 and a.dte_in between b.dte_in and b.dte_out)
最后一行('people_id = 2')时间在第一排时间之间,这并不意味着厨师在正常工作时工作吗? –
您可以添加更多行样本表数据,并调整预期结果吗? – jarlh
@a_horse_with_no_name sql 2008 r2 – Scaver