Oracle 组合两个查询并在列中返回一个结果

Oracle 组合两个查询并在列中返回一个结果,oracle,pivot,oracle-sqldeveloper,transform,oracle12c,Oracle,Pivot,Oracle Sqldeveloper,Transform,Oracle12c,道歉如果这已经被回答了,我看了,但找不到确切的我要找的。我有两组查询 查询1:借项凭证查询我想为每个账号返回1个借项凭证\u无特定偏好哪一个 查询2:信用备忘录查询我希望每个账号返回1张信用备忘录,没有特定的首选项,其中一张根据账号匹配查询1和查询2,并在列而不是行中显示结果。 第一个问题:我需要限制查询1中每个账户的一份借方备忘录和查询2中每个账户的一份贷方备忘录。然后按帐号匹配查询1和查询2,并在列中返回结果。下面的屏幕截图显示了所需的结果 查询1借方凭证查询: Select distinc

道歉如果这已经被回答了,我看了,但找不到确切的我要找的。我有两组查询

查询1:借项凭证查询我想为每个账号返回1个借项凭证\u无特定偏好哪一个 查询2:信用备忘录查询我希望每个账号返回1张信用备忘录,没有特定的首选项,其中一张根据账号匹配查询1和查询2,并在列而不是行中显示结果。 第一个问题:我需要限制查询1中每个账户的一份借方备忘录和查询2中每个账户的一份贷方备忘录。然后按帐号匹配查询1和查询2,并在列中返回结果。下面的屏幕截图显示了所需的结果

查询1借方凭证查询:

Select distinct 
    hca.account_number, hcsu.attribute8, hcsu.attribute9,
    apsa.amount_due_remaining debit_memo_amount, apsa.customer_trx_id, rcta.trx_number
from 
    hz_cust_accounts hca, hz_cust_acct_sites_all hcas, hz_cust_site_uses_all hcsu, ra_customer_trx_all rcta, ar_payment_schedules_all apsa, manar.man_om_consignments mac
where 
    1 = 1
    and hca.cust_account_id = hcas.cust_account_id
    and hca.cust_account_id = rcta.bill_to_customer_id
    and rcta.customer_trx_id = apsa.customer_trx_id
    and hcas.cust_acct_site_id = hcsu.cust_acct_site_id
    and hcsu.SITE_USE_CODE = 'BILL_TO'
    and hcsu.status = 'A'
    and ((apsa.amount_due_remaining > 0 and hcsu.attribute9 = 'CUSTOMER REFUND'))
    and mac.CONSIGNMENT_ID = rcta.attribute2
order by 2, 1, 5;
Select distinct 
    hca.account_number, hcsu.attribute8, hcsu.attribute9,
    apsa.amount_due_remaining credit_memo_amount, apsa.customer_trx_id, rcta.trx_number
from 
    hz_cust_accounts hca, hz_cust_acct_sites_all hcas, hz_cust_site_uses_all hcsu, ra_customer_trx_all rcta, ar_payment_schedules_all apsa, manar.man_om_consignments mac
where 
    1 = 1
    and hca.cust_account_id = hcas.cust_account_id
    and hca.cust_account_id = rcta.bill_to_customer_id
    and rcta.customer_trx_id = apsa.customer_trx_id
    and hcas.cust_acct_site_id = hcsu.cust_acct_site_id
    and hcsu.SITE_USE_CODE = 'BILL_TO'
    and hcsu.status = 'A'
    and ((apsa.amount_due_remaining < 0 and  hcsu.attribute9= 'CUSTOMER REFUND'))
    and mac.CONSIGNMENT_ID = rcta.attribute2
order by 
    2, 1 , 5;
查询2信用备忘录查询:

Select distinct 
    hca.account_number, hcsu.attribute8, hcsu.attribute9,
    apsa.amount_due_remaining debit_memo_amount, apsa.customer_trx_id, rcta.trx_number
from 
    hz_cust_accounts hca, hz_cust_acct_sites_all hcas, hz_cust_site_uses_all hcsu, ra_customer_trx_all rcta, ar_payment_schedules_all apsa, manar.man_om_consignments mac
where 
    1 = 1
    and hca.cust_account_id = hcas.cust_account_id
    and hca.cust_account_id = rcta.bill_to_customer_id
    and rcta.customer_trx_id = apsa.customer_trx_id
    and hcas.cust_acct_site_id = hcsu.cust_acct_site_id
    and hcsu.SITE_USE_CODE = 'BILL_TO'
    and hcsu.status = 'A'
    and ((apsa.amount_due_remaining > 0 and hcsu.attribute9 = 'CUSTOMER REFUND'))
    and mac.CONSIGNMENT_ID = rcta.attribute2
order by 2, 1, 5;
Select distinct 
    hca.account_number, hcsu.attribute8, hcsu.attribute9,
    apsa.amount_due_remaining credit_memo_amount, apsa.customer_trx_id, rcta.trx_number
from 
    hz_cust_accounts hca, hz_cust_acct_sites_all hcas, hz_cust_site_uses_all hcsu, ra_customer_trx_all rcta, ar_payment_schedules_all apsa, manar.man_om_consignments mac
where 
    1 = 1
    and hca.cust_account_id = hcas.cust_account_id
    and hca.cust_account_id = rcta.bill_to_customer_id
    and rcta.customer_trx_id = apsa.customer_trx_id
    and hcas.cust_acct_site_id = hcsu.cust_acct_site_id
    and hcsu.SITE_USE_CODE = 'BILL_TO'
    and hcsu.status = 'A'
    and ((apsa.amount_due_remaining < 0 and  hcsu.attribute9= 'CUSTOMER REFUND'))
    and mac.CONSIGNMENT_ID = rcta.attribute2
order by 
    2, 1 , 5;
预期结果:


这不是一个解决方案,时间太长,无法发表评论。为什么不从客户a/c12000033或任何您知道的有信用备忘录的客户帐号开始,并在过程中进行反向操作和调整呢

 with credit as ( Select distinct hca.account_number,hcsu.attribute8,hcsu.attribute9,apsa.amount_due_remaining,apsa.customer_trx_id, rcta.trx_number
        from hz_cust_accounts hca, hz_cust_acct_sites_all hcas, hz_cust_site_uses_all hcsu, ra_customer_trx_all rcta, ar_payment_schedules_all apsa, manar.man_om_consignments mac
        where 1=1
        and hca.cust_account_id = hcas.cust_account_id
        and hca.cust_account_id = rcta.bill_to_customer_id
        and rcta.customer_trx_id = apsa.customer_trx_id
        and hcas.cust_acct_site_id = hcsu.cust_acct_site_id
        and hcsu.SITE_USE_CODE = 'BILL_TO'
        and hcsu.status = 'A'
        and((apsa.amount_due_remaining < 0 and  hcsu.attribute9= 'CUSTOMER REFUND') )
        and mac.CONSIGNMENT_ID = rcta.attribute2
        and hca.account_number= 12000033
        order by 2, 1 , 5),
    debit as (
    Select distinct hca.account_number,hcsu.attribute8,hcsu.attribute9,apsa.amount_due_remaining,apsa.customer_trx_id, rcta.trx_number
    from hz_cust_accounts hca, hz_cust_acct_sites_all hcas, hz_cust_site_uses_all hcsu, ra_customer_trx_all rcta, ar_payment_schedules_all apsa, manar.man_om_consignments mac
    where 1=1
    and hca.cust_account_id = hcas.cust_account_id
    and hca.cust_account_id = rcta.bill_to_customer_id
    and rcta.customer_trx_id = apsa.customer_trx_id
    and hcas.cust_acct_site_id = hcsu.cust_acct_site_id
    and hcsu.SITE_USE_CODE = 'BILL_TO'
    and hcsu.status = 'A'
    and((apsa.amount_due_remaining > 0 and  hcsu.attribute9= 'CUSTOMER REFUND') )
    and mac.CONSIGNMENT_ID = rcta.attribute2
    and hca.account_number= 12000033
    order by 2, 1 , 5)

    select a.*,b.* from  credit a  -- remove unnecessary columns later
    join debit b
    on (a.account_number=b.account_number)
    and (a.amount_due_remaining+b.amount_due_remaining)=0   -- find matching reverse entry

欢迎来到堆栈溢出。请使用标签下方的编辑按钮编辑您的问题,并解释哪个字段包含借方/贷方金额。我发现唯一的区别在于WHERE子句中倒数第二个比较,但在字段列表中,我不知道哪一个包含贷方/借方金额。会是归因吗?属性9?应付金额/剩余金额?另外,请不要像图片一样发布代码或数据-使用格式化文本,以便人们可以将其复制出来并使用它。一、 坦率地说,我对重新键入数据不感兴趣。谢谢25年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,它的使用不受欢迎。我编辑了我的问题,注意哪一个是贷方备忘录查询和借方备忘录查询,字段名正确,金额_due _remaining这太棒了,我想这给了我很大的帮助几乎是我需要的。。。我将继续处理这个。。我将全速前进。。没有帐号参数。。我只需要每个账号返回一张贷记单,然后每个账号返回一张借记单。。。我可以做一些像max?的事情,首先处理一个客户,一旦您获得所需的输出,请注释客户帐号以扩展搜索。是,使用max amount限制每个客户一份信用备忘录