2

How to use the outer join and or operator together with the conditions mentioned below

Table 1: PERSON

ID_NO         NAME
512           RAM
513           RAVI
514           RAMU

Table 2: FORM_DETAILS

APPL_DATE      STATUS  ID NO 
12/09/2009       A       512
12/08/2010       A       513
12/07/2009       C       514
12/06/2006       S       513 

The query

SELECT A.ID_NO,A.NAME B.APPL_DATE FROM PERSON A, FORM_DETAILS B
WHERE A.ID_NO=B.ID_NO(+) AND
B.APPL_DATE>='12/07/2009' AND
B.APPL_DATE<='12/09/2009' AND
B.STATUS='A' OR
B.STATUS='C'
ORDER BY APPL_DATE

Produces

Error: outer join operation(+) is not allowed in operand of or

Any suggestion is appreciated.

test user
  • 63
  • 7
  • 3
    Your tag `mysql`, but the syntax is oracle. – dnoeth Jul 29 '15 at 10:15
  • Is there supposed to be a - sign prior to 'C' or should this be an = sign? – Steve Matthews Jul 29 '15 at 10:16
  • This is not an answer, but rather a comment on another possible issue. I'm guessing you mean _OR (B.STATUS='A' OR B.STATUS='C')_ as the brackets make this a single predicate. Otherwise this would mean meet all of the conditions before the _OR_, or only meet condition _B.STATUS='C'_. See why here - http://stackoverflow.com/a/1241158/201648. Gordon Linoff's use of _IN ('A', 'C')_ reflects this assumption. – Aaron Newton Jul 30 '15 at 02:11

4 Answers4

0

Just change your query to (if you want to stick with the old (+) notation, discouraged, because it's vendor specific syntax, and deprecated):

SELECT A.ID_NO,
       A.NAME, 
       B.APPL_STATUS
  FROM PERSON A, 
       FORM_DETAILS B
    WHERE A.ID_NO = B.ID_NO(+) 
      AND B.APPL_DATE BETWEEN '12/07/2009' AND '12/09/2009'
      AND B.STATUS IN ('A', 'C')
ORDER BY APPL_DATE

Better, if you want to write ANSI compliant SQL:

SELECT A.ID_NO,
       A.NAME, 
       B.APPL_STATUS
  FROM PERSON A
  LEFT JOIN FORM_DETAILS B ON A.ID_NO = B.ID_NO
    WHERE B.APPL_DATE BETWEEN '12/07/2009' AND '12/09/2009'
      AND B.STATUS IN ('A', 'C')
ORDER BY APPL_DATE
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
0

You should just use explicit join type instead of (+):

SELECT A.ID_NO,A.NAME B.APPL_STATUS 
FROM PERSON A LEFT JOIN FORM_DETAILS B ON A.ID_NO=B.ID_NO
WHERE
B.APPL_DATE BETWEEN '12/07/2009' AND '12/09/2009' AND
(B.STATUS = 'A' OR
B.STATUS = 'C')
ORDER BY APPL_DATE
Bulat
  • 6,869
  • 1
  • 29
  • 52
0

Instead of a syntax which is deprecated for decades better use LEFT JOIN:

SELECT A.ID_NO,A.NAME B.APPL_STATUS FROM PERSON A
LEFT JOIN FORM_DETAILS B
ON A.ID_NO=B.ID_NO AND
B.APPL_DATE>='12/07/2009' AND
B.APPL_DATE<='12/09/2009' AND
B.STATUS='A' OR
B.STATUS='C'
ORDER BY APPL_DATE
dnoeth
  • 59,503
  • 4
  • 39
  • 56
0

Forget the + syntax for joins. Learn to use explicit join syntax. It is much easier and compatible across databases.

You do need to be careful about where to put the conditions. I think you want:

SELECT P.ID_NO, P.NAME, FD.APPL_STATUS
FROM PERSON P LEFT JOIN
     FORM_DETAILS FD
     ON P.ID_NO = FD.ID_NO AND
        FD.APPL_DATE >= DATE '2009-12-07' and DATE '2009-12-09' AND
        FD.STATUS IN ('A', 'C')
ORDER BY FD.APPL_DATE;

Note the use of IN instead of OR. I'm pretty sure this has the right semantics for what you are trying to do.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786