我有以下两个表:
表1:
Id DeviceName DeviceLocation AdditionalColumn1 AdditionalColumn2
1 xyz Africa SomeColText1 SomeText1
2 abc USA SomeColText2 SomeText2
表2:
Id Name ExternalId DeviceName DeviceLocation Version
1 yyy 10 xyz Africa 1
2 bbb 11 xyz Africa 1
3 uuu 10 abc USA 2
我正在尝试提出一个SQ
L,它会从Table2
Table1
中获取Table1(AdditionalColumn1, AdditionalColumn2)
的所有值,并从{{1}获取其他字段}}。另外,我想从Table2
获取具有最大Version
的元素。所以预期的结果应该是:
Id Name ExternalId DeviceName DeviceLocation Version AdditionalColumn1
2 bbb 11 xyz Africa 1 SomeColText1
3 uuu 10 abc USA 2 SomeColText2
我有一个基本的版本设置,但什么是跨数据库的可移植版本?
答案 0 :(得分:1)
我认为这将是可能的解决方案之一:
select t2.Id,
t2.Name,
t2.ExternalId,
t2.DeviceName,
t2.DeviceLocation,
t2.Version,
t1.AdditionalColumn1,
t1.AdditionalColumn2
from
(select ExternalID, max(Version) as Version from Table2 group by ExternalID) tmp
join Table2 t2 on tmp.ExternalID = t2.ExternalID and tmp.Version = t2.Version
join Table1 t1 on t1.DeviceName = t2.DeviceName and t1.DeviceLocation = t2.DeviceLocation