0

我有一个带有日期时间情侣的“检测到的存在”表和一个带有日期时间情侣的“预期存在”表......我想知道我什么时候预期存在但没有......我认为这是与日期时间设置的差异(在 tsql 除了操作数),但我不知道如何用 sql 解决。

如果我不清楚,举个例子:

    CREATE TABLE [dbo].[DetectedPresence](
        [entrance] [datetime] NULL,
        [exit] [datetime] NULL
    )

    CREATE TABLE [dbo].[ExpectedPresence](
        [entrance] [datetime] NULL,
        [exit] [datetime] NULL
    )

INSERT INTO ExpectedPresence VALUES ('2012-11-16 08.15','2012-11-16 12.00')
INSERT INTO ExpectedPresence VALUES ('2012-11-16 17.00','2012-11-16 18.00')
INSERT INTO DetectedPresence VALUES ('2012-11-16 08.00','2012-11-16 12.00')
INSERT INTO DetectedPresence VALUES ('2012-11-16 15.00','2012-11-16 18.00')

预期存在值:

entrance               exit
2012-11-16 08.00       2012-11-16 12.00
2012-11-16 15.00       2012-11-16 18.00

DetectedPresence 值:

entrance               exit
2012-11-16 08.15       2012-11-16 12.00
2012-11-16 15.00       2012-11-16 17.00

我想设置日期时间差异(间隙):

2012-11-16 08.00       2012-11-16 08.15
2012-11-16 17.00       2012-11-16 18.00

你能帮助我吗?谢谢你。

4

1 回答 1

0

类似的东西

case when detectedStart > expectedStart then 
   expectedStart + '-' + detectedStart 
else null end as startDiff,
case when detectedEnd < expectedEnd then 
   detectedEnd + '-' + expectedEnd 
else null end as endDiff

或者

    insert into difference (expectedStart, detectedStart)
    Select
        expectedStart,detectedStart 
    from 
        ExpectedPresence e inner join
        DetectedPresence d on
        e.staffID = d.staffID
    where
       detectedStart > expectedStart;

    insert into difference (expectedEnd, detectedEnd)
    Select  
        expectedEnd,detectedEnd 
    from 
        ExpectedPresence e inner join
        DetectedPresence d on
        e.staffID = d.staffID
    where
        detectedEnd < expectedEnd
于 2012-11-15T19:55:58.910 回答