sql命令显示来自两个相同日期表的时间和超时,由员工代码配对

时间:2014-01-23 05:47:21

标签: sql-server-2008

select Tin.TimeIn,Tin.Date,Tout.TimeOut,tout.Date
from AttendanceTimeIn as Tin
Join AttendanceTimeOut as Tout on Tin.Code=tout.Code
where tout.Code=14 and Tin.Date between `('2014-01-22')` and `('2014-01-23')` 

它的输出是:

Time in     Date            TimeOut Date
07:31       2014-01-22      04:47   2014-01-22
07:31       2014-01-22      10:46   2014-01-23
10:41       2014-01-23      04:47   2014-01-22
10:41       2014-01-23      10:46   2014-01-23

我只想要匹配日期的记录

3 个答案:

答案 0 :(得分:0)

试试这个

Select Tin.TimeIn,Tin.Date,Tout.TimeOut,tout.Date
from AttendanceTimeIn as Tin
Join AttendanceTimeOut as Tout on Tin.Code=tout.Code and Tin.Date = Tout.Date
where tout.Code=14 

<强>(OR)

Select Tin.TimeIn,Tin.Date,Tout.TimeOut,tout.Date
from AttendanceTimeIn as Tin
Join AttendanceTimeOut as Tout on Tin.Code=tout.Code and Tin.Date = Tout.Date
where tout.Code=14 AND Tin.Date between `('2014-01-22')` and `('2014-01-23')`

答案 1 :(得分:0)

尝试这一点,我确保即使没有超时

也会显示Time-in
SELECT  Tin.TimeIn,
        Tin.Date,
        Tout.TimeOut,
        tout.Date
FROM    AttendanceTimeIn As Tin
        Left Outer Join AttendanceTimeOut As Tout
            On Tin.Code=tout.Code
            And Tin.Date = tout.Date
WHERE   Tout.Code=14
        And Tin.Date Between '2014-01-22' And '2014-01-23'

答案 2 :(得分:0)

您的预期输出是否正确?

Time in     Date            TimeOut  Date
07:31       2014-01-22      04:47    2014-01-22 
10:41       2014-01-23      10:46    2014-01-23

您可以在WHERE子句

上添加条件
 WHERE tout.Code=14 and Tin.Date between `('2014-01-22')` and `('2014-01-23')`
  AND (tin.Date = tout.Date);