SQL query to search for pairs that use the same set of values

I had a problem creating a query for this problem.

I have this little table

Tasks(employee_name, task) Sample Data: Tasks ------------------ Joe | taskA Joe | taskB Ted | taskA Jim | taskB Ray | taskA Ray | taskB John| taskA Tim | taskC 

I need to find all pairs of employees who have the same tasks.

For example, using the data above, the result set should be:

 --------------------- employee1 | employee2 --------------------- Joe | Ray Ted | John 

I am using mySQL for the database. Thanks!

+2
source share
2 answers
 select a.employee_name,b.employee_name from tasks as a, tasks as b where a.employee_name>b.employee_name group by a.employee_name,b.employee_name having group_concat(distinct a.task order by a.task)=group_concat(distinct b.task order by b.task) 
+3
source

Attach the table to yourself, choose one name employee_ more than another, and where tasks are equal.

 select emp1.employee_name, emp2.employee_name, emp1.task from tasks emp1 inner join task emp2 on emp1.employee_name > emp2.employee_name and emp1.task = emp2.task 

I hope you have a REAL PK, or this is just an example of exercise. This will not be good in a production environment, as computer_name is not going to uniquely identify an employee in most companies / systems.

+1
source

Source: https://habr.com/ru/post/1235165/


All Articles