Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
hi,
I am trying to compare two tables, I want to select the smartcards that are not in another the other list of smartcards , but the number I get is extremely high , should be around 90 or 120 maximum , but brings me 248k , Table A has 312k records and table B has 478K records , the number of inconsistencies is usually not more than 120 and wanted to know if I'm doing something wrong with my query.

SQL
Select a.SMARTCARD,a.STB,a.AREA,a.ESTATE
From REPORT_CAS a
Where not exists(  select SMARTCARD From REPORT_IW b
                 Where a.smartcard = b.smartcard
                 And a.stb = b.stb)
And a.smartcard  between '1303379968' and '1303969791' 
and len(smartcard) = 10
and ltrim(rtrim(stb)) not in ('0')          
group by a.SMARTCARD,a.STB,a.AREA,a.ESTATE
Posted
Updated 17-Apr-15 13:40pm
v2
Comments
PIEBALDconsult 17-Apr-15 19:37pm    
Dunno, but avoid subqueries and EXISTS/NOTEXISTS -- try a LEFT OUTER JOIN instead.

You can directly use except clause.
 
Share this answer
 
Please, read comments to the question and follow the link provided there.

Try this:
SQL
SELECT a.SMARTCARD,a.STB,a.AREA,a.ESTATE
FROM REPORT_CAS AS a LEFT OUTER JOIN (
    SELECT SMARTCARD
    FROM REPORT_IW
) AS b ON a.stb = b.stb AND a.smartcard = b.smartcard
WHERE a.smartcard BETWEEN 1303379968 AND 1303969791 AND b.stb IS NULL AND b.smartcard IS NULL
 
Share this answer
 
Comments
Laxmax1 20-Apr-15 10:10am    
Hi Maciej, with your solucion i have 61k of registers, but still high :(
Maciej Los 20-Apr-15 10:42am    
So, you need to check SQL server performance, remove unnecessary table indexes, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900