使用ORDER BY和UNION进行ORACLE查询

时间:2012-08-09 18:15:31

标签: java sql oracle sql-order-by

我有以下查询,效果很好!

唯一的问题是我需要DESC Order中的结果集,并且我附加到查询末尾的以下ORDER BY子句会生成以下Oracle错误:

ORA-00933:SQL命令未正确结束 00933. 00000 - “SQL命令未正确结束” *原因:
*行动: 行错误:46列:54

这是查询。同样,此查询有效。只是当我添加以下行时:

FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc

一切都破了。

有什么想法吗? 我相信我的ALIAS与它有关,但我可能是错的。

查询 -

    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;

UPDATE - 我删除了ALIAS并在两个查询之后放置了'ORDER BY'语句,它在SQL Developer中工作;但是,当我通过使用Java生成此查询的Web应用程序执行此查询时,我得到一个关于INVALID SYMBOL的ORACLE ERROR。 无效符号是一个分号,用于终止第一个ORDER BY语句。它在SQL Developer中不起作用,但显然当我执行通过Web App生成的相同语句时 - 它失败了。

4 个答案:

答案 0 :(得分:2)

您需要计算括号 - 要联合的两个查询应该在一个括号中 - 然后您的别名和SELECT * FROM将起作用。

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;

答案 1 :(得分:1)

我认为你正在使用额外的支架')'

 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;

首先删除条件中的最后一个括号,然后尝试。 另外在末尾添加一个括号) FINAL_SEARCH order by

答案 2 :(得分:1)

你忘记了一些括号:

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;

答案 3 :(得分:0)

删除“;”用于填充数据集/数据表的Web应用程序中的符号。

此外,您可以按块顺序使用列顺序作为别名,例如:

选择..... 订购1 asc,4 desc