I have a sql database that querys the histroy data of engines. One of the features is that on a daily basis a stored procedure will search through all the data for a specific event and if the event is found it will insert it into a new table.
The problem is that this data must be unique…
The Table headers are as below:
ID / ID_Location / IDDown / IDUp / DtDown / DtUp / TimeDifference / IsExempt / Reason / Details / OrigDtUp
And the sql query is:
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
But I need the query to check both the DtUp and OrigDtUp columns and ignore the timedifference as the user can change the DtUp to any value in a list and the origional DtUp value moves to OrigDtUp therfore the record is now different and the records is reinserted but with the original values??
Thanks in advance!!
Neil
Use a left join, it’s easier to see the logic: