不同的匹配对 - Teradata/SQL
Distinct Match Pair - Teradata/SQL
我正在尝试加入 2 tables 以在其中找到匹配对,(请参见图片示例)
加上我的加盟条件
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
期望的结果集 --- 因为我想要来自 table 的唯一对,我正在尝试获得
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
这将 return 您的示例数据的正确结果:
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
我正在尝试加入 2 tables 以在其中找到匹配对,(请参见图片示例)
加上我的加盟条件
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
期望的结果集 --- 因为我想要来自 table 的唯一对,我正在尝试获得
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
这将 return 您的示例数据的正确结果:
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