一种方式做到这一点是使用找到的最新datevalidfrom每个ID查询和使用,作为一个派生表,你加入其他表有,像这样:
select * from employee e
join employeeaddress ea on e.id = ea.id
join (select id, max(datevalidfrom) as max_date from employeeaddress group by id) ea2
on ea.id = ea2.id and ea.datevalidfrom = ea2.max_date
通过您限制查询的其余部分派生表与MAX(datevalidfrom)连接(这将让你的所有行)到最新。
并把它变成一个观点:
create view employee_latest_address as
select e.id, e.name, ea.datevalidfrom, ea.addressline1, ea.addressline2, ea.postcode
from employee e
join employeeaddress ea on e.id = ea.id
join (select id, max(datevalidfrom) max_date from employeeaddress group by id) ea2
on ea.id = ea2.id and ea.datevalidfrom = ea2.max_date
来源
2015-06-20 10:49:18
jpw
哎呀,错的别名。不需要额外的连接,这已经足够了 –
谢谢,完美。 –