汇总和分组依据的配置单元表

时间:2020-05-04 15:46:18

标签: hive

我有一个包含以下数据的Hive表。我想将2行中的这6行转换为基于key的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

我想以

    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;

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试以下方法。您需要使用coalesce将空值替换为credit

select
    substr(key, 1, 3) as key,
    coalesce(type, 'credit') as type,
    sum(price1) as price1
from myTable
group by
    substr(key, 1, 3),
    coalesce(type, 'credit')
相关问题