-3

我是 Oracle 顶点和 SQL 的新手。我正在开发一个用于会计的 Oracle 应用程序。我的表ga_voucher包含以下列,其值如下:

VNO | DBIT CODE | AMOUNT | CREDIT CODE  |  VDATE       | NARRATION
01      121005     40000       331001    09/07/2021    ABCD
02      331002    50000        121005    09/07/2021

我想生成具有唯一帐户代码余额的报告(代码的借方总额 - 贷方总额),例如:

Account CODE | DEBIT TOTAL|CREDIT TOTAL | BALANCE
331002           50000    | 40000       |  10000

报表的 SQL 查询是什么?请帮我。

4

1 回答 1

-2

我很确定331001第一行应该是331002. 如果是这样,我认为您想取消数据透视并聚合:

select x.account_code, sum(x.debit) as debit, sum(x.credit) as credit,
       sum(x.debit - x.credit) as balance\
from ga_voucher v cross join lateral
     (select v.debit_code as account_code, v.amount as debit, 0 as credit from dual union all
      select v.credit_code, 0, v.amount from dual
     ) x
group by x.account_code;
于 2021-09-19T12:22:54.877 回答