Search code examples
sqldistinctno-duplicates

select distinct out of distinct


I have two table one has employees goals and the other has list of employees. i have to match one to another. Seems easy to do. but in the employee table employees can be entered more than once with more than one way of spelling their names. How can I pick only one name for each ID, it really doesn't matter which one I pick. this is the code i used:

     select  distinct (etar.EmplKey ), emp.EmplFullName
     FROM EmployeeTarget etar
     inner join DimEmployee emp on emp.emplkey = etar.emplkey
     inner join dimbranch br on br.BranchId = etar.BranchId
     where etar.BranchId = 8

this is the results i get:

     EmplKey    EmplFullName
      100260    Ida Patton
      101488    Don Sheppard
      101488    Donald Sheppard
      101489    Teresa Coverdale
      103121    Harjinder Aujla

How can I have that Don Sheppard guy listed only once?


Solution

  • The easiest way is to do aggreagtion:

     select  etar.EmplKey, min(emp.EmplFullName)
     FROM EmployeeTarget etar
     inner join DimEmployee emp on emp.emplkey = etar.emplkey
     inner join dimbranch br on br.BranchId = etar.BranchId
     where etar.BranchId = 8
     group by etar.EmplKey