我有一个包含三个字段的地址历史记录表:EmpID
、Address
、AddrID
。
每次添加新地址时,我也会AddrID
将该特定员工的地址 ID ( ) 加 1。
EmpID | AddrID | Address
-------------------------------
1 | 1 | 1234 First Ave
1 | 2 | 2145 First Ave
1 | 3 | 1111 First Ave
2 | 1 | 1001 Second St
2 | 2 | 1002 Second St
2 | 3 | 1003 Second St
2 | 4 | 2222 Second St
3 | 1 | 3332 Third Lane
3 | 2 | 3333 Third Lane
4 | 1 | 4444 Fourth Way
如何获取每位员工的最新地址(最高地址 ID)?理想情况下,我应该能够返回:
EmpID | AddrID | Address
-------------------------------
1 | 3 | 1111 First Ave
2 | 4 | 2222 Second St
3 | 2 | 3333 Third Lane
4 | 1 | 4444 Fourth Way
到目前为止,我返回的结果要么太多(即,每个员工、每个 AddrID 1 以及与这两者关联的每个地址),要么返回的结果太少(即,每个具有 AddrID 4 的员工 - 只有员工 2)。
我尝试使用Distinct
, Group By
, Order By
,Having
和 Self-Joins 无济于事。
我错过了什么?