用于合并列的hive sql查询

时间:2018-04-25 13:34:45

标签: sql hive

我在同一个表上运行了两个不同过滤条件的查询:

hive> select * from mailing1 where layer ='db_gold';
OK
db_gold 400
db_gold 500
db_gold 600

hive> select * from mailing1 where layer ='db_insight';
OK
db_insight  100
db_insight  200
db_insight  300

我正在尝试将两个查询组合在一起并生成如下输出:

db_gold 400 db_insight 100
db_gold 500 db_insight 200
db_gold 600 db_insight 300

我已经尝试过" union"两张表如下,没有运气:

select * from mailing1 where layer ='db_insight' 
 union 
select * from mailing1 where layer ='db_gold';
db_gold         400
db_gold         500
db_gold         600
db_insight      100
db_insight      200
db_insight      300

1 个答案:

答案 0 :(得分:0)

看起来JOIN运算符是必需的,但没有用于连接的列。为此,您可以使用表的行号:

SELECT * FROM
    (SELECT row_number() OVER () rn, * FROM mailing1 WHERE layer = 'db_insight') t1
INNER JOIN 
    (SELECT row_number() OVER () rn, * FROM mailing1 WHERE layer = 'db_gold') t2
ON t1.rn = t2.rn;