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
 Find records that don't match in SAME TABLE

Author  Topic 

NolanClark
Starting Member

2 Posts

Posted - 2012-09-15 : 21:21:25
I have a table with 2 fields that looks like this:
Player_ID | Piece_ID
p1 4
p1 12
p2 4
p2 9
p2 5

I want to build a query that identifies the piece_IDs that p1 has that p2 does not, and also the piece_IDs that p2 has that p1 does not.
The results would be:
p1 has Piece_ID 12 that p2 doesn't have.
p2 has Piece_IDs 9 and 5 that p1 doesn't have.

I've found loads of examples to find records in table A that are not in table B, but I just want to find these records from ONE table...

How is this done????
Thanks

NolanClark
Starting Member

2 Posts

Posted - 2012-09-15 : 21:50:16
Found it.... In case anyone else is looking:

select distinct(piece_ID)
from tbl_Player_Pieces
where Player_ID='p1' and Piece_ID not in (select piece_ID from tbl_Player_Pieces where Player_ID='p2')

Relatively simple I guess.
Thanks for looking.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-15 : 21:51:27
[code]
SELECT Piece_ID,MIN(Player_ID) AS Player_ID
FROM Table
WHERE Player_ID IN ('p1','p2')
GROUP BY Piece_ID
HAVING MIN(Player_ID) = MAX(Player_ID)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -