Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Self-Join Query Problem

Status
Not open for further replies.

TroutMask

Technical User
May 31, 2002
1
GB
Hello,

I've got a SQL JOIN problem that I can't figure out. I have a table TABLE_A with the following columns:

CD_ID - Unique record identifier
DATE - Date value
BAND_ID - Identifier of a band stored in another table
USER_ID - An identifier for a specific user

For any given date and band_id, there may be one or more than one user_id. cd_id is the only unique field.

Problem: Given two user_ids and a band_id, I want to find out what dates user 111 has but user 222 does not.

The way I would do this with a sub-select would be: SELECT * FROM TABLE_A WHERE user_id = 111 AND band_id = 444 AND date NOT IN (SELECT date FROM TABLE_A WHERE user_id = 222 AND band_id = 444)

I've gotten close. The following returns a list of records where BOTH users have the date and band_id:

SELECT DISTINCTROW A.* FROM cdlist A LEFT JOIN cdlist B ON A.band_id=B.band_id AND A.date=B.date WHERE A.band_id = 13 AND A.user_id = 1 AND B.user_id = 2

But I'm having a heck of a time trying to get it to do the opposite: show me what they DON'T share.

Any help would be greatly appreciated!

Thanks,

Les
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top