如何根据两个表的联合获得信贷和债务

时间:2018-04-24 14:32:07

标签: sql oracle oracle11g

今天我在编码时面临一个相当奇怪的问题,我真的不知道如何解决这个问题,这是我写的代码

      select kv.third_party,          --THIRD PARTY CODE
             kv.NUM_PUROR,            --PURCHASE CODE
             kv.DTACN_COD_ACN_DTACN   --ACCOUNT CODE
             SUM(DEBT) DEBT,
             SUM(CREDIT) CREDIT
    FROM(SELECT ACNVI.third_party,          
                ACNVI.NUM_PUROR,     
                ACNVI.DTACN_COD_ACN_DTACN
                SUM(ACNVI.AMN_DBT_ACNVI - ACNVI.AMN_CRD_ACNVI) DEBT,
                0 CREDIT
        FROM BFS_ACCOUNT_COUVHER_ITEMS ACNVI
        WHERE TO_CHAR(ACNVI.ACVOH_DAT_DCM_ACVOH,'YYYY') BETWEEN '1990' AND '2000'
        AND SUBSTR(ACNVI.DTACN_COD_ACN_DTACN,1,4) IN ('3102')
    GROUP BY  ACNVI.third_party,          
              ACNVI.NUM_PUROR,     
              ACNVI.DTACN_COD_ACN_DTACN,
    SUBSTR(ACNVI.DTACN_COD_ACN_DTACN,1,4)
    HAVING SUM(ACNVI.AMN_DBT_ACNVI - ANCVI.AMN_CRD_ACNVI)>0
    UNION 
    SELECT ACNVI.third_party,          
                ACNVI.NUM_PUROR,     
                ACNVI.DTACN_COD_ACN_DTACN
                0 DEBT,
                SUM(ACNVI.AMN_CRD_ACNVI- ACNVI.AMN_DBT_ACNVI ) CREDIT
        FROM BFS_ACCOUNT_COUVHER_ITEMS ACNVI
        WHERE TO_CHAR(ACNVI.ACVOH_DAT_DCM_ACVOH,'YYYY') BETWEEN '1990' AND '2000'
        AND SUBSTR(ACNVI.DTACN_COD_ACN_DTACN,1,4) IN ('3102')
    GROUP BY  ACNVI.third_party,          
                ACNVI.NUM_PUROR,     
                ACNVI.DTACN_COD_ACN_DTACN,
    SUBSTR(ACNVI.DTACN_COD_ACN_DTACN,1,4)
    HAVING SUM(ACNVI.AMN_CRD_ACNVI- ACNVI.AMN_DBT_ACNVI)>0) KV
*****
    GROUP BY KV.THIRD_PART,KV.NUM_PUROR,KV.DTACN_COD_ACN_DTACN

好,所以上面是我写的代码和我想要的我不知道该怎么做是这样的:我想在星区有类似的东西 "总和(债务)=总和(信贷)" 但是当我写这个表是空的时候。

参数含义:

#amn_dbt_acnvi   --- amount of debt for each account
#amn_crd_acnvi   --- amount of credit for each account
#(amn_dbt_acnvi-amn_crd_acnvi)    --- account balance (i wrote two kinds in the code because i want to have one balance on debt and one balance on credit)
#bfs_account_voucher_items   ---this is the table with required info (debt,credit,third_party code ,etc.)

    needed result is something like this for example :
    row#1 : third_party: a // num_puror: b//account_code: 310201//debt : 100 credit :0
    row#2 : third_party: a // num_puror: b//account_code: 310203//debt : 0 
credit:100

它们的减去应为0

0 个答案:

没有答案