0

I have a scenario where credit_Date, debit_date and loan_date can be same. Output table have below columns

Date: should combine credit_date, debit_date and loan_date ( credit_date, debit_date and loan_date can be same (or) null)

Credit_payment: Find the sum of credit amount for a given credit_date, entity, currency, owner

Debit_payment: Find the sum of debit amount for a given debit_date, entity, currency, owner

Loan_payment: Find the sum of loan amount for a given loan_date, entity, currency, owner,

entity: values from Table1

currency : values from Table 1

Owner: values from Table 1

Total : sum of ( credit_payment + debit_payement+ loan_payment)

I tried below query but not working

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;

mck
  • 40,932
  • 13
  • 35
  • 50
smp97
  • 63
  • 6

1 Answers1

1

You can use coalesce to combine the three dates before group by. It will take care of the nulls.

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
mck
  • 40,932
  • 13
  • 35
  • 50
  • I have another question for this scenario. Please find the link: https://stackoverflow.com/questions/66528262/hive-hql-for-derived-columns-and-find-the-total @mck – smp97 Mar 08 '21 at 10:40