hive(hql)用于派生列并查找总数

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

你能给我引路吗
我有一个场景,其中credit\u date、debit\u date和loan\u date的值可以不同,也可以相同。输出表有以下列
日期:应结合贷方日期、借方日期和贷款日期(贷方日期、借方日期和贷款日期可以相同(或)有不同的日期)
贷记付款:查找给定贷记日期、实体、货币、所有者的贷记金额之和
借方付款:查找给定借方日期、实体、货币、所有者的借方金额之和
贷款支付:查找给定贷款日期、实体、货币、所有者的贷款金额之和,
实体:表1中的值
货币:表1中的值
所有者:表1中的值
合计:贷方付款+借方付款+贷款付款之和
请查看以下截图。

0dxa2lsx

0dxa2lsx1#

您可以使用与我之前的回答类似的查询,并更改 table1 为了便于按日期分组:

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

相关问题