如何在另一个配置单元查询中使用一个配置单元结果作为条件?

bnlyeluc  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(334)

我有个问题。我有两张Hive桌,第一张有状况。需要在sec查询中动态查找条件。例如,第一个查询:

select col1, col2 from table1. will return account = 'abc'

在第二个查询中,我需要将此用作条件,例如:

select * from table2
where account = 'abc'

有人知道吗?提前谢谢

gj3fmq9x

gj3fmq9x1#

apachehive支持使用join根据相关列合并多个表中的行。
在这个例子中,有一个 accounts table和table orders table。该查询使用一个联接来查找与每个帐户对应的所有订单,并将其过滤为 account1 以及 account2 . 这个 accounts 在本例中,表被简化为只有一列,这可能使它看起来不必要。在实际使用中,accounts表将有多个列,但联接语法保持不变。

查询

WITH
    accounts AS (
        SELECT 'account1' AS account_name UNION ALL
        SELECT 'account2' AS account_name UNION ALL
        SELECT 'account3' AS account_name
    ),
    orders AS (
        SELECT 'account1' AS account_name, 'order1' AS order_name UNION ALL
        SELECT 'account1' AS account_name, 'order2' AS order_name UNION ALL
        SELECT 'account1' AS account_name, 'order3' AS order_name UNION ALL
        SELECT 'account2' AS account_name, 'order4' AS order_name UNION ALL
        SELECT 'account2' AS account_name, 'order5' AS order_name UNION ALL
        SELECT 'account2' AS account_name, 'order6' AS order_name UNION ALL
        SELECT 'account3' AS account_name, 'order7' AS order_name UNION ALL
        SELECT 'account3' AS account_name, 'order8' AS order_name UNION ALL
        SELECT 'account3' AS account_name, 'order9' AS order_name
    )
SELECT
    orders.account_name,
    orders.order_name
FROM accounts
JOIN orders ON accounts.account_name = orders.account_name
WHERE accounts.account_name IN ('account1', 'account2');

结果

account_name    order_name
0   account1    order1
1   account1    order2
2   account1    order3
3   account2    order4
4   account2    order5
5   account2    order6

相关问题