使用具有重复值的列加入2个表;考虑第1排

时间:2015-06-12 06:38:59

标签: sql-server

enter image description here

enter image description here

我想使用EMP列加入SALID表。如果ID表中有Sal个重复,请将第一行与ID sal对应private static UITechnologyElement IterateOnControl(UITechnologyElement parent, string name) { UITechnologyElement te = null; UITechnologyManager TM = Playback.GetCoreTechnologyManager("MSAA"); IEnumerator windowEnumerator = TM.GetChildren(parent, null); windowEnumerator.Reset(); while (windowEnumerator.MoveNext()) { UITechnologyElement current = windowEnumerator.Current as UITechnologyElement; if (current.Name == name) { te = current; break; } } return te; } 。结果应该是:

enter image description here

2 个答案:

答案 0 :(得分:3)

使用窗口函数ROW_NUMBER()

SELECT SAL.ID, SAL.Gross, Sal.Net, Sal.Deductions, Emp.Name FROM EMP JOIN 
(SELECT *, 
 ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Net) rown FROM SAL
) SAL on EMP.ID = SAL.ID
WHERE SAL.rown = 1

答案 1 :(得分:0)

以下是使用row_number窗口函数管理此方法的方法:

select * from emp e
join (select *, row_number() over(partition by id order by(select null)) rn from sal ) s 
  on e.id = s.id and s.rn = 1