SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 join tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vasu4us
Posting Yak Master

102 Posts

Posted - 08/30/2006 :  08:45:14  Show Profile  Reply with Quote
i have 2 tables with a common ID in both tables.
one table has more number of rows than the other
i want to a query which gives the list of ids or rows which are missing in the other table.

left,right,inner jopins only give me common or common + right/left
i want only the missing once.

can someone help me with this

SwePeso
Patron Saint of Lost Yaks

Sweden
30187 Posts

Posted - 08/30/2006 :  08:50:28  Show Profile  Visit SwePeso's Homepage  Reply with Quote
TableA has the "more number" records.

SELECT TableA.*
FROM TableA
LEFT JOIN TableB ON TableB.ID = TableA.ID
WHERE TableB.ID IS NULL

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 08/30/2006 :  09:41:34  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
or this....

SELECT * FROM TABLEA X WHERE NOT EXISTS (SELECT * FROM TABLEB Y WHERE Y.ID = X.ID)


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

vasu4us
Posting Yak Master

102 Posts

Posted - 08/30/2006 :  11:03:39  Show Profile  Reply with Quote
thanks
two solutions worked but when i count the 2 tables the difference is 10006 rows
but when i run this query iam geting 12310 rows. iam not able to justify why this difference
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30187 Posts

Posted - 08/30/2006 :  11:40:15  Show Profile  Visit SwePeso's Homepage  Reply with Quote
You most probably have duplicate IDs somewhere.

SELECT COUNT(DISTINCT ID), COUNT(ID) FROM TableA
SELECT COUNT(DISTINCT ID), COUNT(ID) FROM TableB

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000