试试这个,希望对你有帮助
with table_1 as
(select 10 as tb1_a, 'John' as tb1_b
from dual
union all
select 20 as tb1_a, 'John1' as tb1_b
from dual
union all
select 30 as tb1_a, 'John2' as tb1_b from dual),
table_2 as
(select 10 as tb2_a, 100 as tb2_b
from dual
union all
select 10 as tb2_a, 1000 as tb2_b
from dual
union all
select 10 as tb2_a, 10000 as tb2_b
from dual
union all
select 20 as tb2_a, 200 as tb2_b
from dual
union all
select 20 as tb2_a, 2000 as tb2_b
from dual
union all
select 20 as tb2_a, 20000 as tb2_b from dual),
table_3 as
(select 100 as tb3_a, 'City1' as tb3_b
from dual
union all
select 1000 as tb3_a, 'City10' as tb3_b
from dual
union all
select 10000 as tb3_a, 'City100' as tb3_b
from dual
union all
select 200 as tb3_a, 'City2' as tb3_b
from dual
union all
select 2000 as tb3_a, 'City20' as tb3_b
from dual
union all
select 20000 as tb3_a, 'City200' as tb3_b from dual)
select user_id, city
from (select u.tb1_a as user_id,
nvl(c.tb3_b, 'No city') as city,
rank() over(partition by u.tb1_a order by tb3_a) as city_rank
from table_1 u, table_2 u_c, table_3 c
where u.tb1_a = u_c.tb2_a(+)
and u_c.tb2_b = c.tb3_a(+)) t1
where city_rank = 1