我在oracle中有2个表名为employee_1和mobile_1 empolyee_1数据
Mobile_No Sim_No Start_Date End_Date
1111111113 1111112222 11/10/2017 21:02:44 13/10/2017 21:02:44
1111111111 1212121212 3/10/2017 21:02:44 10/10/2017 21:02:44
1111111111 1111111111 11/10/2017 21:02:44 13/10/2017 21:02:44
1111111111 1111111112 11/10/2017 21:02:44 13/10/2017 21:02:44
1111111111 1111111111 12/10/2017 21:02:44
Mobile_1数据
Mobile_No Status
1111111111 connected
1111111112 connected
我想检索第一个表的所有4列。 条件是empolyee_1.mobile_No = Mobile_1.mobile_no,具有最长开始日期。
答案 0 :(得分:0)
以下是解决方案,请查看...
select mobile_no,sim_no,start_date,end_date from(
select mobile_no,sim_no,start_date,end_date,rank() over(partition by mobile_no,sim_no order by start_date desc) rn from employee_1)s
where rn=1
and exists (select 1 from mobile_1 m where m.Mobile_No = s.mobile_no);
答案 1 :(得分:0)
INNER JOIN
:
select e1.*
from employee_1 e1
inner join ( select mobile_no, max(e.start_date) start_date from employee_1 e group by mobile_no ) e2
on ( e1.mobile_no = e2.mobile_no and e1.start_date = e2.start_date )
inner join mobile_1 m
on ( m.mobile_no = e1.mobile_no );