hive-else合计

bnl4lu3b  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(284)

以下命令在rdbms数据库上作为sql运行良好。但是,它无法在Hive上运行。

SELECT
        entry_date,
        customer,
        cust_loc,
        SUM(run_time) AS TOTAL_RUN,
        SUM(CASE WHEN run_time BETWEEN 10000 AND 20000 THEN entry_date ELSE 0 END) AS SLOT_1,
        SUM(CASE WHEN run_time BETWEEN 200001 AND 30000 THEN entry_date ELSE 0 END) AS SLOT_2,
        SUM(CASE WHEN run_time BETWEEN 31000 AND 40000 THEN entry_date ELSE 0 END) AS SLOT_3
FROM
        cust_run_details
WHERE
        app_env IN ('a','b')
        AND entry_date = '2015-02-01'
        AND flag_set='U'
GROUP BY
        customer,
        customer,
        cust_loc

在hive上运行时遇到的错误是

FAILED: SemanticException [Error 10016]: Line 6:80 Argument type mismatch '0': The expression after ELSE should have the same type as those after THEN: "string" is expected but "int" is found

我怎样才能把它转换成Hive当量?拜托,有什么主意能帮我吗

n6lpvg4x

n6lpvg4x1#

将sum改为count&去掉“else 0”。到那里花了很长时间。叹气。。

SELECT
        entry_date,
        customer,
        cust_loc,
        SUM(run_time) AS TOTAL_RUN,
        COUNT(CASE WHEN run_time BETWEEN 10000 AND 20000 THEN entry_date END) AS SLOT_1,
        COUNT(CASE WHEN run_time BETWEEN 200001 AND 30000 THEN entry_date END) AS SLOT_2,
        COUNT(CASE WHEN run_time BETWEEN 31000 AND 40000 THEN entry_date END) AS SLOT_3
FROM
        cust_run_details
WHERE
        app_env IN ('a','b')
        AND entry_date = '2015-02-01'
        AND flag_set='U'
GROUP BY
        customer,
        customer,
        cust_loc
wz8daaqr

wz8daaqr2#

假设 entry_date 是一个 date ,你可能不想 sum() 是的。如果需要不同范围内运行时间的总和,则需要 select 这样地:

SELECT entry_date,
        customer,
        cust_loc,
        SUM(run_time) AS TOTAL_RUN,
        SUM(CASE WHEN run_time BETWEEN 10000 AND 20000 THEN run_time ELSE 0 END) AS SLOT_1,
        SUM(CASE WHEN run_time BETWEEN 200001 AND 30000 THEN run_time ELSE 0 END) AS SLOT_2,
        SUM(CASE WHEN run_time BETWEEN 31000 AND 40000 THEN run_time ELSE 0 END) AS SLOT_3

如果要计算不同范围内的运行时间,则:

SELECT entry_date,
        customer,
        cust_loc,
        SUM(run_time) AS TOTAL_RUN,
        SUM(CASE WHEN run_time BETWEEN 10000 AND 20000 THEN 1 ELSE 0 END) AS SLOT_1,
        SUM(CASE WHEN run_time BETWEEN 200001 AND 30000 THEN 1 ELSE 0 END) AS SLOT_2,
        SUM(CASE WHEN run_time BETWEEN 31000 AND 40000 THEN 1 ELSE 0 END) AS SLOT_3

如果 entry_date 可以是 NULL 如果你想忽略这些记录,那么你可以包括 and entry_date is not nullWHEN 条件。

相关问题