SQL“除了”仍有问题

时间:2012-05-11 12:03:55

标签: sql sql-server

我有一个查询引擎历史数据的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 

用户可以调整其中一些值,例如DtUp(引擎重新联机的时间)。更改DtUp值后,原始DtUp值将存储在OrigDtUp字段中,TimeDifference将根据DtDown和DtUp时间变化进行更新。

所以我真正需要的是:

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 OR OrigDtUp, dtdown, dtup from exempts

SQL中的except函数不支持我们所知道的

1 个答案:

答案 0 :(得分:2)

除了它两次,你不能吗?

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 
from exempts
except 
select ID_Location, iddown, OrigDtUp, dtdown, dtup 
from exempts