title: ORACLE Query with ORDER BY and UNION
body: <p>I have the following Query which works great!</p>
  
  <p>The only thing is that I need the result set in DESC Order and the following ORDER BY Clause that I have appended to the end of my Query generates the following Oracle Error:</p>
  
  <p>ORA-00933: SQL command not properly ended
  00933. 00000 -  "SQL command not properly ended"
  *Cause:<br>
  *Action:
  Error at Line: 46 Column: 54</p>
  
  <p>Here is the Query.  Again, this query works. It is just that when I add the following line:</p>
  
  <pre><code>FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc
  </code></pre>
  
  <p>Everything breaks.</p>
  
  <p>Any ideas?
  I believe my ALIAS has something to do with it, but I could be wrong.</p>
  
  <p>Query -</p>
  
  <pre><code>    Select * from ( select DISTINCT(DOC_HDR.DOC_HDR_ID), 
      DOC_HDR.INITR_PRNCPL_ID, 
      DOC_HDR.DOC_HDR_STAT_CD, 
      DOC_HDR.CRTE_DT, 
      DOC_HDR.TTL, 
      DOC_HDR.APP_DOC_STAT, 
      DOC1.DOC_TYP_NM, 
      DOC1.LBL, DOC1.DOC_HDLR_URL, 
      DOC1.ACTV_IND  
      from KREW_DOC_TYP_T DOC1, KREW_DOC_HDR_T DOC_HDR   
      where DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' )  and 
          ( DOC1.DOC_TYP_NM =  'PO' or  
            DOC1.DOC_TYP_NM = 'POA' or  
            DOC1.DOC_TYP_NM = 'POC' or  
            DOC1.DOC_TYP_NM= 'POPH' or  
            DOC1.DOC_TYP_NM ='PORH' or  
            DOC1.DOC_TYP_NM = 'POR' or  
            DOC1.DOC_TYP_NM = 'PORT' or  
            DOC1.DOC_TYP_NM = 'POSP' or 
            DOC1.DOC_TYP_NM = 'POV') and 
            DOC_HDR.DOC_HDR_STAT_CD!= 'I' and  
            DOC_HDR.DOC_TYP_ID = DOC1.DOC_TYP_ID ) 
  
      UNION 
  
      Select DISTINCT DOC_HDR.DOC_HDR_ID, 
              DOC_HDR.INITR_PRNCPL_ID, 
              DOC_HDR.DOC_HDR_STAT_CD, 
              DOC_HDR.CRTE_DT, 
              DOC_HDR.TTL, 
              DOC_HDR.APP_DOC_STAT, 
              DOC1.DOC_TYP_NM, 
              DOC1.LBL, 
              DOC1.DOC_HDLR_URL, 
              DOC1.ACTV_IND 
       FROM KREW_DOC_TYP_T DOC1, 
     KREW_DOC_HDR_T DOC_HDR 
     WHERE DOC1.DOC_TYP_NM = 'PO' AND CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = ANY ( 
        Select PPT.FDOC_NBR 
        FROM PUR_PO_T PPT, PL_PURCHASE_LOG_PO_T PPL 
        WHERE PPT.AP_PUR_DOC_LNK_ID = ANY ( 
           Select PRT.AP_PUR_DOC_LNK_ID 
           FROM PUR_REQS_T PRT, 
                KREW_DOC_HDR_T DOC_HDR 
           WHERE CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = PRT.FDOC_NBR AND 
             DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' ) )) FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc;
  </code></pre>
  
  <p>UPDATE - I removed the ALIAS and placed the 'ORDER BY' Statement after both queries and it worked in SQL Developer; however, when I execute this Query through the Web Application that generates this query using Java, I get a ORACLE ERROR about an INVALID SYMBOL.
  The invalid symbol is a semi-colon that use to terminate the 1st ORDER BY Statement.  It won't work otherwise in SQL Developer, but apparently when I execute that same statement that I generate through the Web App - it fails.  </p>