我有换班时间表:
empoyeeid ShiftId PeriodId Description DateFrom DateTo TimeIn TimeOut Night
----------------------------------------------------------------------------------------------------------
5 9 17 Morning 2014-01-01 2014-12-31 09:00:00 12:59:00 0
5 9 18 Night 2014-01-01 2014-12-31 23:00:00 07:00:00 1
和员工交易表:
Employeeid EventDate EventTime EventType
------------------------------------------------
5 2014-01-02 23:04:29 IN
我想从第一张表中选择句号ID
我试过这个
select
periodid
from
shifts
where
timein <= eventtime and timein >= eventtime
and employeeid = 5
答案 0 :(得分:0)
select periodid
from shifts s
inner join employeeTransactions e
on s.employeeid=e.employeeid
where timein <= eventtime and timeout >=eventtime and e.employeeid=5
答案 1 :(得分:0)
尝试
select periodid from shifts join employee on shifts.timein <= employee.eventtime
and shifts.timein >= employee.eventtime and
employee.employeeid=5 and employee.employeeid = shifts.employeeid
答案 2 :(得分:0)
全部谢谢
abs()函数在
中获得最近的时移select PeriodId from shifts
where EmployeeCode = @employeecode
and datefrom <= @INDat and dateto >= @INDat
order by abs((cast(DATEPART(HOUR,timein) as int))-(cast(DATEPART(HOUR,@INDat) as int)))