派生列的配置单元查询

问题描述

请指导一下。

输入表(表 1)包含 credit_date、credit_amount、debit_date、debit_amount、loan_date、loan_amount 等列。

输出表(表 2)包含日期、credit_payment、Debit_payment、Loan_payment 等列。

Date :应该结合所有的 credit_date、debit_date 和 Loan_date 值。

Credit_payment:查找给定 credit_date 的信用金额总和。

Debit_payment:查找给定 debit_date 的借记金额总和。

Loan_payment:求给定loan_date的贷款总额

我尝试了以下查询,但没有奏效。

insert into table2 
select 
date,debit_payment,credit_payment,Loan_payment
 from (
select 
sum(credit_amount) over parttion by credit_date as credit_payment,sum(debit_amount) over parttion by debit_date as Debit_payment
sum(loan_amount) over parttion by loan_date as Loan_payment
from table1
union all
select credit_date as date from table1
union all
select debit_date as date from table1
union all
select payment_date as date from table1
) t

enter image description here

-------------------------------------------- ----------------------------

我有一个场景,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)的总和

请指导一下。

请找到如下截图。

enter image description here

解决方法

您可能需要在联合所有之前明确指定空列:

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