SQL query to exclude rows within same table with duplicate field values -


i'll try brief.

i have single table need data from, contacts.

there company field , can in either company or company b.

all contacts in company b have duplicate in company a, can matched column legacyid.

what i'm trying company rows not have in company b same legacyid.

i've tried and legacyid not in (select legacyid contacts company = 'b') and not exists (select legacyid contacts company = 'b').

neither of above work because there 260,000 company contacts , 96,000 company b. not in , not exists worked when there less 50,000 rows total, data migration, query timing out or running forever.

thanks

if understand correctly, not exists should want:

select c.* contacts c companyid = 'a' ,       not exists (select 1                   contacts c2                   c2.legacyid = c.legacyid , c2.companyid = 'b'                  ); 

for performance, want index on contacts(legacyid, companyid).


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -