I have a business rule that employees cannot purchase items from employees of the same department. I have two tables. one is the list of employees and their IDs:
Emp_ID Emp_Name Dept_ID
1 John 1
2 Bob 1
3 Susie 2
4 Jack 3
5 Jill 3
And a table of the employee ID and the employee ID they purchased from:
Emp_ID Bought_From_Emp_ID
1 2
2 3
4 5
5 1
My expected output would be to have the the employee id (or name) of both employees if one purchased an item from the same department:
Emp_ID Bought_From_Emp_ID Same_Dept_ID
1 2 1 --John and Bob are in Same Department (1)
4 5 3 --Jack and Jill are in Same Department (3)
How would I do this for millions of records? I have a feeling that this is very simple in the long run, but my mind hasn't shifted towards the solution yet.
I am using Teradata, but can use MSSQL if there are any SQL-specific answers