sql join-在连接期间将值从一个表Map到另一个表中的等价表

ltskdhd1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(220)

我有以下场景:

Table 1 - Holds product meta data about products.
Table 2 - Holds sales data about the products.

每个表都有一个唯一的产品id,但是由于历史原因,表中的id是不同的。例如:

In Table 1, "Red Hat" -> 1
In Table 2, "Red Hat" -> 3.

我想将表2连接到表1,并根据产品idMap行。
有没有办法“Map”这些值,以便表2中id为3的所有行都Map到表1中id为1的所有行?

pb3skfrl

pb3skfrl1#

使用 case 连接中的语句 ON 用于Map的子句。
演示。
准备表格:

hive> create table testt1 as select 1 as key;
hive> create table testt2 as select 3 as key;

连接用例:

select t1.key, t2.key 
   from testt1 t1 
        left join testt2 t2 
        on t2.key=case when t1.key=1 then 3 --add more cases 
                      --when t1.key=<some value> then <mapped value>
                        else t1.key          --default mapping t1.key=t2.key 
                  end
  ;

结果:

OK
1       3
Time taken: 41.191 seconds, Fetched: 1 row(s)

相关问题