2011-02-22 4 views
0

을 비교하여 상태를 만들기 위해 2005어떻게 SQL 서버를 사용하여 날짜

남기기 표

이벤트 표

ID Date PresentDate Status 

001 03/30/2010 03/30/2010 Present 
001 03/31/2010 null  absent 
001 04/01/2010 null  Leave 
001 04/02/2010 null  Leave 
001 04/03/2010 null  absent 
001 04/04/2010 04/04/2010 Present 
…. 

모든 Datecolumn 데이터 타입

ID StartDate EndDate 

001 04/01/2010 04/02/2010 
002 04/02/2010 04/03/2010 
… 

은에서 날짜

입니다 상태 열, 존재하는 날이 null의 경우는 「결」으로 표시해, null가 아닌 경우는 displa y를 "present"로 지정하십시오. 이제 날짜에 휴가를 적용하면 상태 열에 '휴가'로 표시됩니다.

쿼리

Select 
    id, date, present date 
    , CASE WHEN t2.id IS NULL THEN t1.Status ELSE ‘Leave’ END AS status 
from event table as t1 
left outer join leave table as t2 on 
    t1.id = t2.id and t1.date between t2.startdate and t2.enddate 

위의 방법이 작동,하지만 난 또 하나 개의 조건을 추가해야합니다. 현재 날짜 열은 다음과 같이 표시되어야 비어있는 경우 우리가, 다음은 현재 날짜 열을 비교해야 나가기 표에서 특정 직원에 대한 휴가 적용되면 경우

예상 출력 "을두고"

ID Date PresentDate Status 

001 03/30/2010 03/30/2010 Present 
001 03/31/2010 null  absent 
001 04/01/2010 null  Leave 
001 04/02/2010 null  Leave 
001 04/03/2010 null  Leave (Expect this value) 
001 04/04/2010 04/04/2010 Present 
…. 

위 출력에서 ​​벗어남은 2010 년 4 월 1 일부터 2010 년 4 월 2 일까지이며, 현재 날짜의 다음 열은 null이고 상태는 "Leave"로 표시되어야합니다. 현재 날짜가 null이 아니면 "현재"로 표시되어야합니다.

위의 조건에 대한 쿼리를 만드는 방법 방법

We can display as "Leave" in status column from Start Date to end date of leave table, after that leave date end then we can compare with PresentDate column, if PresentDate column is null then it should display as "Leave", once data is available in present column then status should display with normal condition. 

.

쿼리 도움말

답변

2
select E.id, E.date, E.presentdate, *, 
    case 
    when E.presentdate is not null then 'Present' 
    when E2.presentdate is not null then 'Absent' 
    when L.ID is not null then 'Leave' 
    else 'Absent' 
    end 
from Event E 
outer apply (
    select top 1 * 
    from Leave L 
    where E.presentdate is null and E.date >= L.startdate 
     AND e.ID = L.ID 
    order by L.startDate desc) L 
outer apply (
    select top 1 * 
    from Event E2 
    where E.presentdate is null 
     and E2.presentdate is not null 
     and E.date >= E2.date and E2.date > L.startdate  
     AND e2.ID = e.ID 
    order by E2.presentdate desc) E2 
order by E.date 

휴가 테이블

ID   StartDate    EndDate 
----------- ----------------------- ----------------------- 
1   2010-04-01 00:00:00.000 2010-04-02 00:00:00.000 
1   2010-04-02 00:00:00.000 2010-04-03 00:00:00.000 
1   2010-04-05 00:00:00.000 2010-04-05 00:00:00.000 

출력

id   date     presentdate    
----------- ----------------------- ----------------------- ------- 
1   2010-03-30 00:00:00.000 2010-03-30 00:00:00.000 Present 
1   2010-03-31 00:00:00.000 NULL     Absent 
1   2010-04-01 00:00:00.000 NULL     Leave 
1   2010-04-02 00:00:00.000 NULL     Leave 
1   2010-04-03 00:00:00.000 NULL     Leave -** 
1   2010-04-04 00:00:00.000 2010-04-04 00:00:00.000 Present 
1   2010-04-05 00:00:00.000 NULL     Leave 
1   2010-04-06 00:00:00.000 NULL     Leave -** 
1   2010-04-07 00:00:00.000 NULL     Leave -** 
1   2010-04-08 00:00:00.000 2010-04-08 00:00:00.000 Present 
1   2010-04-09 00:00:00.000 NULL     Absent 
1   2010-04-10 00:00:00.000 NULL     Absent 
1   2010-04-11 00:00:00.000 2010-04-11 00:00:00.000 Present 

것들은 표시된 필요 - ** 남기기 기록에 의해 덮여 것이 아니라, 그들이 휴가를 보여 그들이 때문에 휴가 기간에 따라, 맞습니까? 예를 들어, 2010-04-09은 현재 레코드를 따르기 때문에 (실제로 존재하지 않고) "부재"로 남아 있습니다.

+0

나는 empid의 n 개의 숫자를 가지며 쿼리도 이벤트 테이블 empid = leave table empid를 비교하지 않으며 날짜 만 비교합니다. – Gopal

+0

@ 리차드, 회신 해 주셔서 감사합니다. 귀하의 질문을 시도했지만 제대로 표시되지 않고 있습니다. 휴가가 시작되면 "휴가"로 표시되고, 휴가 날짜가 끝난 후 휴가 대신 "부재 중"으로 표시됩니다. – Gopal

+0

@Gopal - 수정 된 답변보기 – RichardTheKiwi