Sql – Select all rows that equal on same column (unknown) value

sql

My table lists threads between x users. Each user can be in y threads.

Below, user_id 1 is in thread 1 and 2, and so on…

Now, lets say user_id 1 is given only, how can I get all rows that have the same thread_id as the row that has user_id 1?

       id   user_id  thread_id
        1     1        1    
        2     1        2    
        3     2        1    
        4     3        2    

Have no clue…


EDIT

Given are the user_uids 1 and 2. Now get me the thread_id that only both of them share. I dont want all threads of 1 and 2 but only the one that they share together.

Best Answer

If you want to find pairs of users that have exactly the same set of threads, then the following query will work:

select t.user_id, t2.user_id
from (select t.*, count(*) over (partition by t.user_id) as NumThreads
      from t
     ) t left outer join
     t t2
     on t.thread_id = t2.thread_id and t.user_id <> t2.user_id
group by t.user_id, t2.user_id
having SUM(case when t.user_id is null then 1 else 0 end) = 0 and
       SUM(case when t2.user_id is null then 1 else 0 end) = 0 and
       count(*) = max(t.NumThreads)

In this query, wherever it says "from t t2" you would put "from t2". Where the line is "from t" you would put "from t".

The structure of this query is a self-join with an aggregation. The intention to to find every pair of matching threads. Then to group them by the two user_ids. If the number of matching threads is the number of threads for the user (the third condition) and no threads fail to match (the first two conditions), then the two users match on all their threads.

Your question in the comments about threads on all users is easier.

select thread_id
from t cross join
     (select count(distinct user_id) as NumUsers from t) const
group by thread_id
having count(*) = max(const.NumUsers)