views:

78

answers:

2

I tried using "Exist" and "IN". Not only did I not succeed, it didn't seem as an efficient solution.
Here is a simplified example:

TblMyTable
UserName1  -  Grade  -  UserName2  -  Grade

I need a query where there is a mutual relation / existence.
What I mean is that the returned result from the query will only include the users where on the same row there is both UserName1 and UserName2 mutually (see image below for a better example / explanation).

Any user can work with any other user.
So the result will be (the order doesn't matter) ideally in one line:
John - 5000  --  Mary - 3000
or
Mary - 3000  --  John - 5000

The punchline is, it's one dynamically changing table with active Users using the F.Key to the main User's table, which has the P.Key.

Please see image below for a better example / explanation.
Database is SQL 2005.

Many thanx in advance

* Edit: Screenshot that hopefully help explain it all.
The end result should be 2 rows, because only they have a mutual relation in TblDynamicUserList:

ana - Phone - 3000   ---   RanAbraGmail - Wifi - 2000
and
anaHeb - Phone - 5000   ---   RoyP - Phone - 4000

+2  A: 

Would something like this work for you:

With ManagerWorkers As
    (
    -- get managers with workers
    Select Managers.WorkerUsername As ManagerUsername, Workers.WorkerUsername
    From tblMyTable As Managers
        Join tblMyTable As Workers
            On Workers.ManagerUsername = Managers.WorkerUsername
    ) 
Select *
From ManagerWorkers
Union All
-- get workers that have a manager in the above list
Select WorkerUsername, ManagerUsername
From tblMyTable
Where Exists(   Select 1
                From ManagerWorkers
                Where ManagerWorkers.ManagerUsername = tblMyTable.ManagerUsername
                )   

EDIT: Given the update to the question how about the following query:

Select D1.u_username, U1.Permission, U1.Grade, D1.f_username, U2.Permission, U2.Grade
from tblDynamicUserList As D1
    Join tblDynamicUserList As D2
        On D2.u_username = D1.f_username
            And D2.f_username = D1.u_username
    Join tblUsers As U1
        On U1.u_username = D1.u_username
    Join tblUsers As U2
        On U2.u_username = D2.u_username
Thomas
Thomas I apologize, missed you by a few minutes :) I re-edited the question to make it clearer and hopefully simpler
ranabra
I like Thomas' solution better than mine and if you add the following it will eliminate the duplicates.WHERE D1.U_userName < D1.f_username
Turnkey
Wow, thank you both, Thomas and Turnkey. it works nicely :)One small thing I do not understand. _username is a string value / varchar. How can you use a "Lower than <" operator on such a string in the WHERE clause? -->D1.U_userName < D1.f_username
ranabra
SQL will interpret the less than or greater than symbol with respect to the items alphabetic order. E.g. A is less than B. It is a bit more complicated than that when you account for collation but that is the simple explanation.
Thomas
BTW, if a solution works for you, go ahead and mark it as the answer.
Thomas
+1  A: 

This is a bit involved of a query but works as requested. It basically joins the two tables twice and then compares where there are matches on both sides. The comparison clause makes sure that duplicates are not returned by the "NOT" in the final where clause.

SELECT F.UserU, F.UserV
FROM
  (SELECT U.u_userName AS UserU, V.u_username AS UserV
    FROM TblUsers U INNER JOIN TblDynamicUserList D 
    ON U.u_username = D.U_username
    INNER JOIN TblUsers V On V.u_username = D.F_UserName) F,
  (SELECT U.u_userName AS UserU, V.u_username AS UserV
    FROM TblUsers U INNER JOIN TblDynamicUserList D 
    ON U.u_username = D.U_username
    INNER JOIN TblUsers V On V.u_username = D.F_UserName) G
WHERE (F.UserU = G.UserV AND F.UserV = G.UserU AND F.UserU < F.UserV) 
AND  NOT (F.UserU = G.UserV AND F.UserV = G.UserU AND F.UserU > F.UserV) 
Turnkey