select distinct SD.RefNo, MIN(Datefrom) E_DateFrom ,EmploymentStatusID
from StudentEmploymentHistory SEH
RIGHT JOIN SDetail SD ON SD. StudentID = SEH.StudentID
我希望能够仅将一条记录作为结果,但是由于雇佣状态有多个记录,所以我现在有多个记录
我的结果,只需要带有最小日期起于的记录
答案 0 :(得分:0)
使用窗口功能:
select sd.RefNo, seh.E_DateFrom, seh.EmploymentStatusID
from SDetail sd left join
(select seh.*,
row_number() over (partition by seh.StudentID order by Datefrom) as seqnum
from StudentEmploymentHistory SEH
) seh
on seh.StudentID = sd.StudentID and seh.seqnm = 1;