不同的匹配对 - Teradata / SQL

时间:2015-09-09 15:52:22

标签: sql teradata

我正在尝试加入2个表来查找匹配对,(例如,请参见图片)

加入条件

table1 
left join (or inner join)
table2 
on table1.metric1 = table2.metric1
and table1.metric2 = table2.metric2
and table1.metric3 = table2.metric3

我得到的结果集是

A 1 , A 2, B 1, B 2 

期望的结果集---因为我想从我想要获得的表中获得唯一的对

A 1 , B 2 as my final result set.

有人可以帮我实现吗? 我尝试了Rank,Partition by但没有任何作用。 谢谢,

table 1         
ID  Metric1 Metric 2    Metric 3
A   x        y           z
B   x        y           z
C   p        q           r





table 2         
ID  MEtric1     Metric2 Metric3
1   x            y        z
2   x            y        z
3   l            m        n

1 个答案:

答案 0 :(得分:0)

这将返回示例数据的正确结果:

select *
from 
 (
   select t.*,
      row_number()
      over (partition by metric1, metric2, metric3
            order by ID) as rn1
   from table1 as t
 ) as t1
join
 (
   select t.*,
      row_number()
      over (partition by metric1, metric2, metric3
            order by ID) as rn2
   from table2 as t
 ) as t2
 on t1.metric1 = t2.metric1
and t1.metric2 = t2.metric2
and t1.metric3 = t2.metric3
and t1.rn1     = t2.rn2