오늘까지 EntryTime 열에 항목이있는 다음과 같은 테이블이 있습니다.
1 번 테이블
PersonID|StartTime|EndTime|EntryTime
1 |10:00 |12:00 |2014-01-01
1 |10:00 |12:00 |2014-01-02
1 |10:00 |12:00 |2015-01-03
...
및 계약 테이블 :
표 2
PersonID|ContractID| ContractStart| ContractType|Hours
1 |1 |2014-01-01 |Student |8
1 |2 |2015-01-01 |Trainee |40
1 |3 |2015-03-01 |Student |20
1 |4 |2015-05-01 |Student |12
이제 아래에 언급 된 결과 테이블을 사용하여 (datediff(minute, StartTime, Endtime))
현재 시간을 합산하고 싶습니다 contractType
. 는 contracttype
마지막에 연속 시작해야합니다. 따라서이 예에서는 계약 3 + 4의 모든 항목이 관련됩니다.
내 생각은 a를 사용 loop
하여 관련 계약을 선택하는 것이 었 습니다.
WHILE (Select ContractType from Table2) = 'Student'
Begin
Select ContractStart from Table2
Order By ContractStart
End
불행히도 루프는 아직 작동하지 않으며 where 절에 구현하는 방법을 모르겠습니다.
내가 얻으려는 결과 테이블 은 2015-03-01부터 시작하는 모든 항목 이있는 것 subset
입니다.Table1
예없이 원하는 것이 무엇인지 알기는 어렵지만 시도입니다. 또한 유형에 대해 많은 추측을하십시오.
참고 : while 루프 와 커서를 피하고 논리를 설정하십시오 =)
declare @Table1 as table
(
PersonID int
,StartTime datetime
,EndTime datetime
,EntryTime date
)
insert into @Table1
values
(1 ,'10:00', '12:00', '2014-01-01')
,(1 ,'10:00', '12:00', '2014-01-02')
,(1 ,'10:00', '12:00', '2015-01-03')
,(2 ,'10:00', '12:00', '2015-01-03')
,(2 ,'10:00', '12:00', '2015-01-04')
,(3 ,'10:00', '12:00', '2015-01-03')
,(3 ,'10:00', '12:00', '2015-01-05')
declare @Table2 as table
(
PersonID int
,ContractID int
,ContractStart date
,ContractType varchar(10)
,[Hours] float
)
insert into @Table2
values
(1 ,1 ,'2014-01-01', 'Student', 8)
,(1 ,2 ,'2015-01-01', 'Trainee', 40)
,(1 ,3 ,'2015-03-01', 'Student', 20)
,(1 ,4 ,'2015-05-01', 'Student', 12)
,(2 ,1 ,'2014-01-01', 'Student', 8)
,(2 ,2 ,'2015-01-01', 'Trainee', 40)
,(2 ,3 ,'2015-03-01', 'Student', 20)
,(2 ,4 ,'2015-05-01', 'Student', 12)
,(3 ,1 ,'2014-01-01', 'Student', 8)
,(3 ,2 ,'2015-01-01', 'Trainee', 40)
,(3 ,3 ,'2015-03-01', 'Student', 20)
,(3 ,4 ,'2015-05-01', 'Student', 12)
--Current contrat (for PersonId = 1)
declare @CurrentType varchar(10) =
(select top 1 ContractType from @Table2 where PersonID = 1 order by ContractStart desc)
--Last different type start (for PersonId = 1)
declare @LastDiff date =
(select top 1 ContractStart from @Table2 where PersonID = 1 and @CurrentType != ContractType order by ContractStart desc)
--Current type span (for PersonId = 1)
select datediff(minute, min(StartTime + cast(EntryTime as datetime)), max(EndTime + cast(EntryTime as datetime))) as [PesonId = 1 TimeSpan]
from @Table1
where PersonID = 1
and EntryTime > @LastDiff
-- Puting everything togheter
select t1.PersonId, datediff(minute, min(t1.StartTime + cast(t1.EntryTime as datetime)), max(t1.EndTime + cast(t1.EntryTime as datetime))) as TimeSpan
from @Table1 t1
where t1.EntryTime >
(
select top 1 t2.ContractStart
from @Table2 t2
where t2.PersonID = t1.PersonID
and t2.ContractType !=
(
select top 1 t2b.ContractType from @Table2 t2b where t2b.PersonID = t2.PersonID order by t2b.ContractStart desc
)
order by t2.ContractStart desc
)
group by t1.PersonId
스 니펫은 설명이 필요 없으며 다음을 제공합니다.
PersonId TimeSpan
-------- --------
1 120
2 1560
3 3000
참고 더 나은 테스트 예제를 만들기 위해 몇 가지 데이터를 추가했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다