组合2个查询并在列中返回1个结果

问题描述 投票:0回答:1

抱歉,如果已经回答了,我看了一下,但是找不到我想要的东西。我有2组查询

问题1:借项凭单查询我想为每个Account_number返回1个借项凭单(没有具体的偏好设置) 查询2:贷项凭单查询我想为每个Account_number返回1个贷项凭单(无特定首选项),按“帐号”匹配查询1和2,并在列而不是行中显示结果。

[第一期-我需要限制查询1的每个帐户的借项凭证和查询2的每个帐户的贷项凭证。然后按帐号匹配查询1和查询2,并在列中返回结果。欲望结果图片附上。

Query 1:
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
order by 2, 1 , 5;


Query 2
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
order by 2, 1 , 5;

期望结果: [列结果中每个帐户的借项凭单和贷项凭单] [1] [1]:https://i.stack.imgur.com/wQxZq.png

oracle pivot oracle-sqldeveloper transform oracle12c
1个回答
0
投票

这不是解决方案,而且评论时间太长。为什么不从客户a / c#12000033或您知道有贷项通知单的任何客户帐号开始,然后逐步进行调整。

 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
© www.soinside.com 2019 - 2024. All rights reserved.