Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Join tables

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2013-04-23 : 11:08:55
Hi there,

Im trying to extract the info that is not matching from two different tables:

Each table has 15 columns, in which they have a common ID_Col.

I need to extract all data from Table2 if the ID_Col doesn't match with Id_Col from table1.

By using this query:

SELECT Table1.Id_Col, Date FROM Table1
LEFT OUTER JOIN Table2
ON Table2.Id = Table1.Id
GROUP BY Table1.Id_Col, Date

I have all the info and NULL values if they are not matching.. instead of NULL values I will need the info.

Any tip?

Thanks

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-04-23 : 11:36:58
Can you show us table descriptions, some data and expected output
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-04-23 : 11:42:07
Sure:

Table_F
Col_Name, Col_Place, Col_Zip, Col_Car, Col_House, Col_Id

Table_S
Col_Name, Col_Date, Col_End, Col_Car, Col_Mind, Col_Id

Extraction:

All the info from Table_S if the Col_id is not matching Col_Id from Table_F

This way I will short out all the clients that i miss in Table_F but they are in Table_S

Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-23 : 12:49:50
multiple ways

1.
SELECT *
FROM Table_S s
WHERE NOT EXISTS (SELECT 1 FROM Table_F WHERE Col_Id = s.Col_Id)

2.
SELECT s.*
FROM Table_S s
WHERE Col_Id NOT IN (SELECT Col_Id FROM Table_F)

3.
SELECT s.*
FROM Table_S s
LEFT JOIN Table_F f
ON f.Col_Id = s.Col_Id
WHERE f.Col_Id IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -