neilrudds
neilrudds

Reputation: 215

SQL "Except" Still having problems

I have a SQL database that queries the history 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 

A user is able to make adjustments to some of these values such as the DtUp (The Time when the engine is back on-line). Once the DtUp value is changed the original DtUp value is stored in OrigDtUp field and the TimeDifference is updated according to DtDown and DtUp time variation.

So what I really need is this:

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

Which we know is not supported by the except function in SQL??

Upvotes: 1

Views: 216

Answers (1)

Nikola Markovinović
Nikola Markovinović

Reputation: 19346

Couldn't you except it twice?

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

Upvotes: 2

Related Questions