0

我有一个查询引擎历史数据的 sql 数据库。其中一个特点是,存储过程每天都会在所有数据中搜索特定事件,如果找到该事件,它将把它插入到一个新表中。

问题是这些数据必须是唯一的......

表头如下:

ID / ID_Location / IDDown / IDUp / DtDown / DtUp / TimeDifference / IsExempt / Reason / Details / OrigDtUp

sql查询是:

insert into exempts(ID_Location, iddown, idup, dtdown, dtup, timedifference)
select ID_Location, iddown, idup, dtdown, dtup, DATEDIFF(MINUTE, dtdown, dtup) as timedifference
from @tmptbl
except
select ID_Location, iddown, idup, dtdown, dtup, timedifference from exempts

但是我需要查询来检查DtUpOrigDtUp列并忽略时间差异,因为用户可以将 更改为DtUp列表中的任何值并且原始DtUp值移动到OrigDtUp记录现在不同并且重新插入记录但使用原始值? ?

提前致谢!!尼尔

4

1 回答 1

2

使用左连接,更容易看出逻辑:

   insert into exempts(ID_Location, iddown, idup, dtdown, dtup, timedifference) 
    select t.ID_Location, t.iddown, t.idup, t.dtdown, t.dtup, DATEDIFF(MINUTE, dtdown, dtup) as timedifference 
    from @tmptbl as t
      left join exempts as e 
        on  t.ID_Location = e.ID_Location 
        and t.iddown = e.iddown
        and t.idup = e.idup
        and t.dtdown = e. dtdown
        and t.DtUp = e.OrigDtUp --  <<  Match diff columns here
    where t.ID_Location is null
于 2012-05-10T11:54:36.610 回答