Hi,
I have a problem with duplicate rows. There are two tables - Emp and Rep.
EMP table
EmpName Rep1 Rep2 Rep3
John 1 2 null
Jim 1 2 3
REP table
RepNo RepName
1 Aril
2 Ben
3 Cate
I want the result as
EmpName Rep1 Rep2 Rep3
John Aril Ben null
Jim Aril Ben Cate
When I use a normal join between Reps in Emp table and Repno in Rep table, the result is duplicate rows like below.
EmpName Rep1 Rep2 Rep3 repNo
John Aril null null 1
John null Ben null 2
Jim Aril null null 1
Jim null Ben null 2
Jim null null Cate 3
The SQL I am using is:
Select empName,
case rep1 when r.repNo then r.repName End as Rep1,
case rep2 when r.repNo then r.repName End as Rep2,
case rep3 wehn r.repNo then r.repName End as Rep3,
repNo FROM Emp
left join Rep r on emp.Rep1 = r.repNo OR
emp.Rep2 = r.repNo OR
emp.Rep3 = r.repNo
Will very much appreciate ideas on getting the result in 2 rows please.
thanks
saj
I have a problem with duplicate rows. There are two tables - Emp and Rep.
EMP table
EmpName Rep1 Rep2 Rep3
John 1 2 null
Jim 1 2 3
REP table
RepNo RepName
1 Aril
2 Ben
3 Cate
I want the result as
EmpName Rep1 Rep2 Rep3
John Aril Ben null
Jim Aril Ben Cate
When I use a normal join between Reps in Emp table and Repno in Rep table, the result is duplicate rows like below.
EmpName Rep1 Rep2 Rep3 repNo
John Aril null null 1
John null Ben null 2
Jim Aril null null 1
Jim null Ben null 2
Jim null null Cate 3
The SQL I am using is:
Select empName,
case rep1 when r.repNo then r.repName End as Rep1,
case rep2 when r.repNo then r.repName End as Rep2,
case rep3 wehn r.repNo then r.repName End as Rep3,
repNo FROM Emp
left join Rep r on emp.Rep1 = r.repNo OR
emp.Rep2 = r.repNo OR
emp.Rep3 = r.repNo
Will very much appreciate ideas on getting the result in 2 rows please.
thanks
saj