【问题标题】:Combine 2 queries and return one result in columns合并 2 个查询并在列中返回一个结果
【发布时间】:2020-10-01 19:21:13
【问题描述】:

抱歉,如果这个问题已经得到解答,我查看了但找不到我正在寻找的确切内容。我有 2 组查询

  • 查询 1:借记通知单查询我想为每个 Account_number 返回 1 个借记通知单(没有具体的偏好)
  • 查询 2:贷项通知单查询我想为每个 Account_number 返回 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;

查询 #2 贷项凭单查询:

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;

想要的结果:

【问题讨论】:

  • 欢迎来到 Stack Overflow。请使用标签正下方的edit 按钮编辑您的问题,并说明哪个字段包含借方/贷方金额。我看到在WHERE 子句的倒数第二个比较中,唯一的区别是&lt;&gt;,但是在您的字段列表中,我不知道哪个包含贷方/借方金额。会是attribute8吗? attribute9? amount_due_remaining?另外,请不要像图片一样发布代码或数据 - 使用格式化文本,以便人们可以将其复制出来并使用它。坦率地说,我对重新输入您的数据没有兴趣。谢谢。
  • Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI JOIN 语法-92 SQL 标准(25 多年前),不鼓励使用它
  • 我已经编辑了我的问题,指出哪个是贷项通知单查询和借项通知单查询,字段名称正确(amount_due_remaining)

标签: oracle pivot oracle-sqldeveloper transform oracle12c


【解决方案1】:

这不是一个解决方案,而且评论时间太长。为什么不从客户 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

【讨论】:

  • 这太棒了,我认为它几乎可以满足我的需求...我将继续使用此功能..我将完全开放..(没有帐号参数)..我只需要为每个 account_number 返回 1 个贷项通知单,然后为每个 account_number 返回 1 个借项通知单...我可以执行“max”之类的操作?
  • 首先在一个客户上工作,一旦您获得所需的输出,请注释掉客户帐号以扩大搜索范围。是的,使用最大金额来限制每个客户一个贷项通知单
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 2014-07-07
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多