2016-10-11 6 views
1

요리사없이 일할 때 사람들의 목록을 만들고 싶습니다.사람들의 SQL 날짜 - 시간 비교

SQL 테이블 :

people_id clock_in    clock_out 
1   2016-10-10 06:58:01.000 2016-10-10 14:59:01.000 
1   2016-10-11 06:57:02.000 2016-10-11 14:58:23.000 
.. 
2   2016-10-10 07:51:01.000 2016-10-10 13:00:01.000 

People_id : 2가

1이 작동 될 때

가 지금은 시간을 표시하려면이 예에서는 1 = 일반 작업 남자와 2 = 요리사가 아닌 경우 하나의 작업 남자와 한 채널이

people_id start_working_without_chef end_working_without_chef 
1   2016-10-10 06:58:01.000 2016-10-10 07:51:01.000 
1   2016-10-10 13:00:01.000 2016-10-10 14:59:01.000 

이 예에서이 같이

ef하지만이 작업은 serveral working mens 및 serveral chief에게 확장되어야합니다.

어떻게이 문제를 해결할 수 있습니까?

+0

마지막 행 ('people_id = 2')은 첫 번째 행 시간 사이에 있으므로 보통 사람이 작업 할 때 요리사가 일하는 것은 아닙니다. –

+0

샘플 테이블 데이터 행을 추가하고 예상 결과를 조정할 수 있습니까? – jarlh

+0

@a_horse_with_no_name sql 2008 r2 – Scaver

답변

0

글쎄, 실제로는 "멋지다"고 생각하지 않습니다. 내일 더 나은 해결책을 찾으려고 노력할 것입니다.

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) 
+0

입력 해 주셔서 감사합니다! 하지만 어떻게하면 내가 요리사 그룹과 비교하기를 원하는 그룹의 사람들을 가질 수 있을까요? 그래서 나는 요리사없이 일하는 사람이있는 시대를 보여줄 수 있습니다. – Scaver