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.
| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-02-16 : 10:17:39
|
| Hi,I have a table as follows, and what I want to do is SELECT "double matches". For instance in the sample data below 100,500 would be a double match. (both users have selected other users as a match)I thought this was simple, but I'm stuck on how to proceed from here. Any help is much appreciated... Thanks again!:)mike123CREATE PROCEDURE dbo.[select_DoubleMatches] ( @userID [int] )SELECT * FROM [tblMatches] WHERE userID = @userID AND ...tblMatchesuserID,matchID100,200100,300100,500500,100 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 10:22:08
|
| [code]declare @tblmatches table (userid smallint, matchid smallint)insert @tblmatchesselect 100, 200 union allselect 100, 300 union allselect 100, 500 union allselect 500, 100select a as player1, b as player2from ( select userid as a, matchid as b from tblmatches union all select matchid, userid from tblmatches ) as xgroup by a, bhaving count(*) > 1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 10:24:28
|
| [code]declare @tblmatches table (userid smallint, matchid smallint)insert @tblmatchesselect 100, 200 union allselect 100, 300 union allselect 100, 500 union allselect 500, 100select t1.*from @tblmatches as t1inner join @tblmatches as t2 on t2.userid = t1.matchid and t2.matchid = t1.userid[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 10:30:42
|
| [code]declare @tblmatches table (userid smallint, matchid smallint)insert @tblmatchesselect 100, 200 union allselect 100, 300 union allselect 100, 500 union allselect 500, 100select distinct case when t1.userid < t1.matchid then t1.userid else t1.matchid end as userid, case when t1.userid > t1.matchid then t1.userid else t1.matchid end as matchidfrom @tblmatches as t1inner join @tblmatches as t2 on t2.userid = t1.matchid and t2.matchid = t1.useridwhere t1.userid = @userid[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-16 : 10:37:16
|
| [code]declare @tblmatches table (userid smallint, matchid smallint)insert @tblmatchesselect 100, 200 union allselect 100, 300 union allselect 100, 500 union allselect 500, 100declare @userid intselect @userid = 500select distinct case when t1.userid = @userid then t1.userid else t1.matchid end as userid, case when t1.userid = @userid then t1.matchid else t1.userid end as matchidfrom @tblmatches as t1inner join @tblmatches as t2 on t2.userid = t1.matchid and t2.matchid = t1.userid[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-02-16 : 10:48:07
|
| Hi Peter,Thanks once again for all your help! I used the third example, however I removed a little bit. I wasn't sure exactly the need for the case structure, and with the data I had in my DB it was transposing the userID and matchID. I don't think for my application I can see any reason why it matters if the userID is bigger than the matchID.I'm left with this and it seems to be working perfectly.Thanks again! and have a great weekend :)mike123 create PROCEDURE dbo.[select_DoubleMatches] ( @userID [int] )AS SET NOCOUNT ON select distinct t1.userID as userID,t1.matchUserID as matchUserIDfrom tblMeetMe_Matches as t1inner join tblMeetMe_Matches as t2 on t2.userID = t1.matchUserID and t2.matchUserID = t1.userIDwhere t1.userID = @useridGO |
 |
|
|
|
|
|
|
|