hive查询派生列并查找派生列的总数

gfttwv5a  于 2021-07-13  发布在  Hadoop
关注(0)|答案(1)|浏览(440)

我有一个场景,其中贷方日期、借方日期和贷款日期可以相同。输出表有以下列
日期:应结合贷方日期、借方日期和借款日期(贷方日期、借方日期和借款日期可以相同(或为空)
贷记付款:查找给定贷记日期、实体、货币、所有者的贷记金额之和
借方付款:查找给定借方日期、实体、货币、所有者的借方金额之和
贷款支付:查找给定贷款日期、实体、货币、所有者的贷款金额之和,
实体:表1中的值
货币:表1中的值
所有者:表1中的值
合计:贷方付款+借方付款+贷款付款之和
我试过了,但没用

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, entity, owner, currency
    union all
    select debit_date as date, null as credit_payment, sum(debit_amount) as debit_payment, null as loan_payment, entity, owner, currency
    from table1
    group by debit_date, entity,owner, currency 
    union all
    select loan_date as date, null as credit_payment, null as debit_payment, sum(loan_amount) as loan_payment, entity, owner, currency
    from table1
    group by loan_date, entity, owner, currency
) t
order by date;

am46iovg

am46iovg1#

你可以用 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, debit_date, loan_date), entity, currency, owner

相关问题