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

时间:2017-07-07 19:10:19

标签: hadoop hive

我有一个问题。我有两个蜂巢表,第一个有条件。需要动态查找sec查询中的条件。 例如 第一个查询:

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

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

select * from table2
where account = 'abc'

任何人都有一些想法?提前谢谢

1 个答案:

答案 0 :(得分:0)

Apache Hive支持使用join根据相关列组合多个表中的行。

在此示例中,有一个accounts表和一个orders表。该查询使用联接来查找与每个帐户对应的所有订单,过滤为account1account2。在此示例中,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