首页 > 解决方案 > 带有 Sum 和 group By 的 Hive 表

问题描述

我有一个带有以下数据的 Hive 表。我想根据键将这 6 行转换为 2 行,并希望使用 type='credit' 的行。

    key          type    price1
    111          null      1000
    111-21      credit    2000
    111-31      credit    3000
    222         credit    1000
    222-21      null      2000
    222-31      null      3000

我想 O/p 作为

    key type  price1
    111 credit 6000
    222 credit 6000

我已经尝试过以下查询,但没有得到所需的输出,有人可以帮我怎么做。

    select key,type,sum(price1) from tablename where type='credit' group by substr(key,3), type;

标签: hive

解决方案


select key, type, sum(price1) from tablename where type = "credit" group by substr(key,1,3)


推荐阅读