I have two tables, T1 and T2, every table has ten columns: teacher1, teacher2, teacher3, teacher4, teacher5, student1, student2, student3, student4, student5.
How to get the rows where T1 and T2 meets the following two rules?
In the rules student* means student1 student2 ... teacher* means teacher1 teacher2...
- T1 and T2 are different, which means at least one of T1's values is not in the T2.
(Despite the column order, T1 teacher1 can appear in T2 student* or teacher* column)- At least one of T1 teacher* is in T2 teacher* OR at least one of T1 student* is in T2 student*, which means that T1 and T2 has at least a common teacher or student.
for example if T1 has one row
C2      NULL    NULL    NULL    NULL    S1      NULL    NULL    NULL    NULL
and T2 has the rows
NULL    NULL    NULL    S1      NULL    NULL    C2      NULL    NULL    NULL
S1      NULL    NULL    C3      NULL    C2      NULL    NULL    NULL    NULL
NULL    NULL    NULL    C2      NULL    S2      NULL    S3      NULL    NULL
the expected results is only the third row of T2 because:
- the first row meet rule2 but not rule1.
- the second row meet rule1 but not rule2.
- the third row meet rule1 and rule2.
I wanted to use the CharIndex function, but every table has more than one million rows, it's incredibly slow.
 
     
     
    