如何在两个表之间进行连接,但仅限于满足连接条件的第一行?
在这个简单的示例中,我想为 table_A 中的每一行获取 table_B 中满足条件的第一行:
select table_A.id, table_A.name, table_B.city
from table_A join table_B
on table_A.id = table_B.id2
where ..
table_A (id, name)
1, John
2, Marc
table_B (id2, city)
1, New York
1, Toronto
2, Boston
The output would be:
1, John, New York
2, Marc, Boston
可能是Oracle 提供了这样的功能(性能是一个问题)。
原文由 kkung 发布,翻译遵循 CC BY-SA 4.0 许可协议
我使用分区来分隔 id2,然后只取 r_num = 1。