tsql - Trying to convert a "not in" statement to a "not exists" -- I've looked at related questions but still can't get it to work -
i've got simple query ex-boss left me with. it's using not in takes enormous amount of time (and reasons yet determined have caused failover on server running 1 database). i've tried change not exists , i'm getting 0 records returned. realize has simple there absolutely no 1 here has clue i'm talking about.
here's query:
declare @callback table (prospectrelationcode varchar(1), callbackavail int) insert @callback select opp.ppc prospectrelationcode, sum(case when dl.category = 'refusal' 1 else 0 end) callbackavail callresult cr join opportunity opp on opp.id = cr.id join dbo.dispositionlookup dl on dl.lookupkey = cr.callstatus dl.category = 'refusal' , opp.assignment not in ( select assignment callresult cr join opportunity opp on opp.id = cr.id cr.callstatus in ('vvol','rvol') ) , opp.listid >= 1400 group opp.ppc here (slight) rework:
select opp.ppc prospectrelationcode, sum(case when dl.category = 'refusal' 1 else 0 end) callbackavail callresult cr join opportunity opp on opp.id = cr.id join dbo.dispositionlookup dl on dl.lookupkey = cr.callstatus dl.category = 'refusal' , not exists ( select opp.assignment callresult cr join opportunity on cr.id = opp.id cr.callstatus in ('vvol','rvol') ) , opp.listid >= 1400 group opp.ppc this piece of cake i'm hitting brick wall here. i'd appreciate whatsoever (and i'm prepared yelling @ me also).
thank you! diane
there's need subquery because joins data in same way first join statement. since data not in subquery. move subquery's clause , negate so:
declare @callback table ( prospectrelationcode varchar(1) ,callbackavail int ) insert @callback select opp.ppc prospectrelationcode ,sum(case when dl.category = 'refusal' 1 else 0 end) callbackavail callresult cr inner join opportunity opp on opp.id = cr.id inner join dbo.dispositionlookup dl on dl.lookupkey = cr.callstatus dl.category = 'refusal' , cr.callstatus != 'vvol' , cr.callstatus != 'rvol' , opp.listid >= 1400 group opp.ppc
Comments
Post a Comment