2017-02-16 72 views
0

我有表中的一個象下面這樣:比較兩個表,並選擇值在表1中不存在(基於時間)

TimeIn  TimeOut 
------------------- 
07:30  12:30 
13:30  16:30 

我有表中的兩個象下面這樣:

TimeIn  TimeOut 
------------------- 
07:00  07:06 
07:10  08:30 
08:45  12:45 
13:55  15:30 
16:15  16:50 

表之一員工的換班政策和表二是員工的出勤時間。

我想知道這位員工在輪班政策時間外的時間是什麼時間?

預期輸出:

TimeIn  TimeOut 
------------------- 
08:30  08:45 
13:30  13:55 
15:30  16:15 

我想這是不工作的查詢:

SELECT InTime AS TimeIn, OutTime AS TimeOut FROM HR.GetRegisterList(@EmpId, @Date) 
EXCEPT 
SELECT FromTime AS TimeIn, ToTime AS TimeOut FROM HR.GetEmployeeDateShiftDetails(@EmpId, @Date) 
+0

那麼你到目前爲止嘗試過什麼? –

+0

我將用我的查詢更新問題。 –

+0

你是如何得到'期望的輸出' - 超時時間到達時間,反之亦然? – Abhishek

回答

0

下面的代碼片段應該做的工作,我不得不做出的修改Shift Policytable - 增加了一個columnShiftType -

declare @shiftpolicy table(time_in varchar(10),time_out varchar(10),ShiftType NVARCHAR(2)) 
insert into @shiftpolicy values ('07:30','12:30','M'),('13:30','16:30','A') 
declare @attendance table(time_in varchar(10),time_out varchar(10)) 
insert into @attendance values ('07:00','07:06'),('07:10','08:30'),('08:45','12:45'),('13:55','15:30'),('16:15','16:50') 
select * from @shiftpolicy 
select * from @attendance 
SELECT * FROM 
(
    select case 
       when row = 1 then 
        case when time_in > DefTimeIn then DefTimeIn--+' - '+convert(nvarchar(50),time_in) 
        else '' 
        end 
       when row = last 
        then case when time_out < DefTimeOut then time_out--+' - '+convert(nvarchar(50),DefTimeOut) 
        else '' 
        end 
       else '' 
        end--* 
      [Out] 
       ,case 
       when row = 1 then 
        case when time_in > DefTimeIn then time_in 
        else '' 
        end 
       when row = last 
        then case when time_out < DefTimeOut then DefTimeOut 
        else '' 
        end 
       else '' 
        end--,* 
       [IN] 
    --select * 
    from 
    (
     select last_value(row) over (partition by ShiftType order by ShiftType) last ,* 
     from 
     (
      select ts.time_in DefTimeIn,ts.time_out DefTimeOut,ts.ShiftType,ta.*,ROW_NUMBER() OVER(PARTITION BY ts.time_in,ShiftType ORDER BY ta.time_in) row 
       ,lead(ta.time_in,1,0) over (PARTITION BY ts.time_in,ShiftType ORDER BY ta.time_in) TimeOt 
      from @shiftpolicy ts 
      inner join @attendance ta 
       on ta.time_in <= ts.time_out and ta.time_out >= ts.time_in 
     )t 
    )ta 
    --order by ShiftType desc 

    union 

    select time_out,lead from 
    (
     select ts.time_in DefTimeIn,ts.time_out DefTimeOut,ts.ShiftType,ta.*,ROW_NUMBER() OVER(PARTITION BY ts.time_in,ShiftType ORDER BY ta.time_in) row 
      ,lead(ta.time_in,1,0) over (PARTITION BY ts.time_in,ShiftType ORDER BY ta.time_in) lead 
      --,lag(ta.time_in,1,0) over (PARTITION BY ts.time_in,ShiftType ORDER BY ta.time_in) 
      --,last_value(ta.time_out) over(PARTITION BY ts.time_in,ShiftType ORDER BY shiftType) 
     from @shiftpolicy ts 
     inner join @attendance ta 
      on ta.time_in <= ts.time_out and ta.time_out >= ts.time_in 
    )t 
)TA 
where ([OUT] <> '1900-01-01 00:00:00.000' AND [IN] <> '1900-01-01 00:00:00.000') AND [out] <> '' AND [in] <> '0' 
order by 1 
相關問題