使用join从2个表中获取不同的数据

时间:2016-06-28 07:07:02

标签: sql-server join

以下是我的查询。我从Employee表中获取socail sercurity no并将其与child_excel表匹配。使用它我获得员工ID并将其与EmployeeCh表(具有员工ID)匹配。这个员工有2个孩子,所以当我提到性别条款时,我为每个孩子的每个孩子提供4个记录2,每个孩子有不同的(即男性,女性)。对于每个有各自性别的孩子,我只想为孩子1排2行。

SELECT distinct 
      [SSN],
      empch2.GenderID,
      gen.Name1
  FROM [child_excel] as t
  INNER JOIN Employee as empch on t.SSN = empch.SocialSecurityNo
  INNER JOIN EmployeeCh  as empch2 on empch.ID = empch2.EmployeeID  
  INNER JOIN Gender as gen on empch2.GenderID = gen.ID

我正在获得O / p

12345  1  Male
12345  2  Female
99999  1  Male
99999  2  Female

预期输出

12345   1   Male
99999   2   Female

但是当我在连接中添加First Name时,它会提供正确的输出。我想要使​​用名字

SELECT distinct 
          [SSN],
          empch2.GenderID,
          gen.Name1
      FROM [child_excel] as t
      INNER JOIN Employee as empch on t.SSN = empch.SocialSecurityNo
      INNER JOIN EmployeeCh  as empch2 on empch.ID = empch2.EmployeeID and  t.First_Name= empch2.FirstName  
      INNER JOIN Gender as gen on empch2.GenderID = gen.ID

1 个答案:

答案 0 :(得分:0)

使用ROW_NUMBER()

Get-ChildItem "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" -Recurse | 
    Where Name -like '*{90140000-001*' | foreach {
    $_.GetValue("DisplayName")
}