2017-07-25 5 views
1

同じ一致基準を持つ複数の行があっても、1回だけ結合する方法はありますか?私は名前と地域に参加したいだけですが、Countを1つの行にのみ適用したい(特定の順序なし)。複数の一致基準でのSQL結合

enter image description here

答えて

2

これは奇妙な要求ですが、row_number()使用することができます。

:あなたは table2の値をカウントしている場合は、しかし、

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; 

は、その後、はるかに簡単な方法があります

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はまったく必要ありません。

関連する問題