2017-07-25 15 views

回答

2

這是一個奇怪的要求,但你可以使用row_number()

select t2.*, 
     (case when row_number() over (partition by name, region order by id) = 1 
      then t1.count 
     end) as count 
from table2 t2 join 
    table1 t1 
    on t2.name = t1.name and t2.region = t1.region; 

但是,如果你是在table2計數的值,那麼還有一個更簡單的方法:

select t2.*, 
     (case when row_number() over (partition by name, region order by id) = 1 
      then count(*) over (partition by name, region) 
     end) as count 
from table2 t2; 

Table1根本不需要。