微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

派生列的 Hive 查询并找到派生列的总数

如何解决派生列的 Hive 查询并找到派生列的总数

我有一个场景,credit_Date、debit_date 和loan_date 可以相同。输出表有以下列

日期:应该结合credit_date、debit_date和loan_date(credit_date、debit_date和loan_date可以相同(或)为空)

Credit_payment:查找给定信用日期、实体、货币、所有者的信用金额总和

Debit_payment:查找给定借记日期、实体、货币、所有者的借记金额总和

Loan_payment:查找给定贷款日期、实体、货币、所有者的贷款金额总和,

实体:来自表 1 的值

货币:表 1 中的值

所有者:表 1 中的值

总计:(credit_payment + debit_payement+loan_payment)的总和

我试过下面的查询但没有用

insert into table2 
select *
from (
    select credit_date as date,sum(credit_amount) as credit_payment,null as debit_payment,null as loan_payment,entity,owner,currency
    from table1
    group by credit_date,currency
    union all
    select debit_date as date,null as credit_payment,sum(debit_amount) as debit_payment,currency
    from table1
    group by debit_date,currency 
    union all
    select loan_date as date,sum(loan_amount) as loan_payment,currency
    from table1
    group by loan_date,currency
) t
order by date;

解决方法

您可以使用 coalesce 组合分组依据前的三个日期。它将处理空值。

select coalesce(credit_date,debit_date,loan_date) as date,sum(credit_amount) as credit_payment,sum(debit_amount) as debit_payment,sum(loan_amount) as loan_payment,entity,currency,owner,sum(credit_amount) + sum(debit_amount) + sum(loan_amount) as Total
from table1
group by coalesce(credit_date,loan_date),owner

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。