3

我需要帮助来加入下面的两个查询。我是甲骨文的新手。我浏览了这里的帖子,但提到的连接是用于简单查询。请帮助我并排显示以下两个查询的结果。

(select Branch, count(*) as "No of Accounts Opened"
      from (SELECT A.CUST_AC_NO,
                   A.CCY,
                   A.branch_code Branch,
                   A.ACY_CURR_BALANCE,
                   A.AC_OPEN_DATE,
                   B.CUSTOMER_NAME1,
                   C.LIMIT_AMOUNT,
                   D.ACCOUNT_CLASS,
                   D.DESCRIPTION
              FROM STTM_CUST_ACCOUNT  A,
                   STTM_CUSTOMER      B,
                   getm_facility      C,
                   STTM_ACCOUNT_CLASS D,
                   getm_liab_cust     e
             WHERE B.CUSTOMER_NO = A.CUST_NO
               AND A.ACCOUNT_CLASS = D.ACCOUNT_CLASS
               and c.liab_id = e.liab_id
               and e.customer_no = b.customer_no
               and e.customer_no = a.cust_no
               AND B.LIABILITY_NO = e.customer_no
               AND RTRIM(C.LINE_CoDe) || LTRIM(TO_CHAR(C.LINE_SERIAL)) =
                   RTRIM(A.LINE_ID)
               AND A.RECORD_STAT = 'O'
               and c.record_stat = 'O'
               and e.record_stat = 'O'
                  --AND to_char(A.AC_OPEN_DATE,'YYYY')=:Year
                  --AND trim(to_char(A.AC_OPEN_DATE,'Month'))=:Month
                  --AND a.BRANCH_CODE ='001'
               AND A.CCY <> 'ZWD'
            UNION
            SELECT A.CUST_AC_NO,
                   A.CCY,
                   Branch_code Branch,
                   A.ACY_CURR_BALANCE,
                   A.AC_OPEN_DATE,
                   B.CUSTOMER_NAME1,
                   NULL LIMIT_AMOUNT,
                   D.ACCOUNT_CLASS,
                   D.DESCRIPTION
              FROM STTM_CUST_ACCOUNT  A,
                   STTM_CUSTOMER      B,
                   STTM_ACCOUNT_CLASS D
             WHERE B.CUSTOMER_NO = A.CUST_NO
               AND A.ACCOUNT_CLASS = D.ACCOUNT_CLASS
               AND A.RECORD_STAT = 'O'
                  --AND to_char(A.AC_OPEN_DATE,'YYYY')=:Year
                  --AND trim(to_char(A.AC_OPEN_DATE,'Month'))=:Month
                  --AND BRANCH_CODE ='001'
               AND A.CCY <> 'ZWD')
     group by Branch
     order by Branch) A

第二个查询是

(select Branch,count(*) as "No of Accounts Closed"  from(SELECT
    a.branch_code Branch,
    A.CUST_AC_NO,
    A.CCY,
    A.ACY_CURR_BALANCE,
    a.maker_id,
    a.maker_dt_stamp,
    a.checker_id,
    A.CHECKER_DT_STAMP,
    B.CUSTOMER_NAME1,
    C.ACCOUNT_CLASS,
    C.DESCRIPTION
    FROM
    STTMS_CUST_ACCOUNT A,
    STTMS_CUSTOMER B,
    STTMS_ACCOUNT_CLASS C
    WHERE
    B.CUSTOMER_NO = A.CUST_NO
    AND A.ACCOUNT_CLASS = C.ACCOUNT_CLASS
    AND A.RECORD_STAT = 'C'
    ---AND A.BRANCH_CODE ='001'- :brn
    --AND trunc(to_char(A.CHECKER_DT_STAMP,'YYYY'))=:Year
    --AND trim(to_char(A.CHECKER_DT_STAMP,'Month'))=:Month
    ORDER BY
    CUSTOMER_NAME1)group by Branch order by Branch) B
4

2 回答 2

6

这些查询之间唯一的共同列是branch,我将假设您想要每个分支机构关闭和打开的帐户数量。

首先,这些查询有很多无关信息;只选择你需要的。其次,您应该真正使用显式连接。它们更加清晰,消除了可能发生错误的明显点,并且作为 SQL 标准已经存在了几十年。

对您的问题的简单快速的回答是:

select a.*, b.*
  from query1 a
  full outer join query2 b
    on a.branch = b.branch

我使用full outer join它是因为不能保证一个查询中的分支存在于另一个查询中。当我可以详细介绍时,我不喜欢快速和简单......

最好的办法是将您的 where 条件删除到sum(case when...)您的select. 这使您可以只扫描您的表两次而不是三次,然后避免full outer join像现在必须做的那样做。

如果我把这些连接弄错了,请原谅我;将它们转换为显式连接语法可能会出错。我已经删除了你的一些 where 条件来加入,因为它们应该在那里。

顺便说一句,您不必担心查询“复杂”。本质上,无论您的查询多长时间,您都可以将其视为最简单的;请注意您认为应该返回哪些数据,并准备好调查它是否看起来有误。

select branch
     , sum(opened) as "number of accounts opened"
     , sum(closed) as "number of accounts closed"
   from ( select branch
               , sum(case when a.record_stat = 'C' then 1 
                          else 0 end) as closed
               , sum(case when a.record_stat = 'O' and a.ccy <> 'zwd' then 1 
                          else 0 end ) as opened
            from STTMS_CUST_ACCOUNT A
            join STTMS_CUSTOMER B
              on B.CUSTOMER_NO = A.CUST_NO
            join STTMS_ACCOUNT_CLASS C
              on A.ACCOUNT_CLASS = C.ACCOUNT_CLASS
              -- include where condition to use indexes (if possible)
           where a.record_stat in ('C','O')
           group by branch
              -- union would imply a distinct, which we don-t want.
           union all
          select branch
               , count(*) as opened
               , 0 as closed
            from STTM_CUST_ACCOUNT A
            join STTM_CUSTOMER  B
              on B.CUSTOMER_NO = A.CUST_NO
            join getm_facility C
              on rtrim(C.LINE_CoDe) || ltrim(to_char(C.LINE_SERIAL)) 
                 = rtrim(A.LINE_ID)
              -- moved from where as this is a join condition
             and a.record_stat = c.record_stat
            join STTM_ACCOUNT_CLASS D
              on A.ACCOUNT_CLASS = D.ACCOUNT_CLASS
            join getm_liab_cust e
              on c.liab_id = e.liab_id
             and e.customer_no = b.customer_no
             and e.customer_no = a.cust_no
             and b.liability_no = e.customer_no
              -- moved from where as this is a join condition
             and a.record_stat = e.record_stat
              -- only want one number returned so conditions in the where clause
           where A.RECORD_STAT = 'O'
             and A.CCY <> 'ZWD'
           group by branch
                 )
 group by branch

抱歉,我懒得用类似的方式处理所有事情。

延伸阅读:

于 2012-06-06T08:44:16.617 回答
1
select 
    branches.branch_code,
    first_query."No of Accounts Opened",
    second_query."No of Accounts Closed"
from
   (select distinct branch_code from STTMS_CUST_ACCOUNT) branches
   left outer join (
       -- first query here
   ) first_query on branches.branch_code = first_query.Branch
   left outer join (
       -- second query here
   ) second_query on branches.branch_code = second_query.Branch

因为您没有为分支提供单独的表,所以我们必须select distinct在第一个别名中执行。如果您有另一个表仅包含此信息,则最好使用它。

我应该补充一点,您的查询似乎可以简化(看起来您并没有使用您获取的所有字段)。

于 2012-06-06T08:25:24.020 回答