Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

sql - How do I convert a "legacy" left outer join statement in Oracle?

I have two tables (A and G) in an Oracle database that can be joined together based off an account number. The one caveat to this is that one of the tables (G) has about 80 fewer records than the other. When I query the two tables together, I need to get all of the rows, so that we see NULL data in the columns for the missing 80 rows.

I currently have an Oracle statement that performs a left outer join query using the following "legacy" syntax:

SELECT A.AccountNo,
       A.ParcelNo,
       A.LocalNo,
       A.PrimaryUseCode, 
       A.DefaultTaxDistrict,
       RTRIM(G.Section),
       RTRIM(G.Township),
       RTRIM(g.Range)

  FROM tblAcct A, tblAcctLegalLocation G

 WHERE A.verstart <= '20100917999' AND A.verend > '20100917999' AND A.DefaultTaxDistrict = '2291' 
       AND (SUBSTR(A.AccountNo,1,1) = 'R' or SUBSTR(A.AccountNo,1,1)= 'I') 
       AND SUBSTR(a.ParcelNo,1,1)<> '7' and substr(a.ParcelNo,1,1)<>'8'
       AND A.AcctStatusCode IN ('A', 'T', 'E') 
       AND A.AccountNo = G.AccountNo(+)
       AND G.verstart(+) <= '20100917999' and G.verend(+) > '20100917999'
ORDER BY A.ParcelNo, A.LocalNo

I'm trying to convert this query into a "standard" LEFT JOIN type query since I'm told the newer versions of Oracle support this syntax. I've tried the basic

LEFT OUTER JOIN ON A.AccountNo = G.AccountNo 

but this doesn't seem to work. My queries wind up returning 80 rows fewer than the full amount.

Can anybody tell me what I'm missing or how to format the query properly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

  SELECT a.AccountNo,
         a.ParcelNo,
         a.LocalNo,
         a.PrimaryUseCode, 
         a.DefaultTaxDistrict,
         TRIM(g.Section),
         TRIM(g.Township),
         TRIM(g.Range)
     FROM tblAcct A
LEFT JOIN tblAcctLegalLocation g ON g.accountno = a.accountno
                                AND g.verstart <= '20100917999' 
                                AND g.verend > '20100917999'
    WHERE a.verstart <= '20100917999' 
      AND a.verend > '20100917999' 
      AND a.DefaultTaxDistrict = '2291' 
      AND SUBSTR(a.AccountNo,1,1) IN ('R', 'I') 
      AND SUBSTR(a.ParcelNo,1,1) NOT IN ('7', '8')
      AND a.AcctStatusCode IN ('A', 'T', 'E') 
 ORDER BY a.ParcelNo, a.LocalNo

Everything you see marked with the (+) must be included in the OUTER join criteria. In an outer JOIN, the criteria is applied before the join.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...