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;
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