admin

使用ORDER BY和UNION进行ORACLE查询

sql

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

唯一的问题是,我需要按DESC顺序排列的结果集,并且我附加到查询末尾的以下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;

更新-我删除了ALIAS,并在两个查询之后都放置了“ ORDER BY”语句,并且该语句在SQL
Developer中有效。但是,当我通过使用Java生成此查询的Web应用程序执行此查询时,出现有关无效符号的ORACLE错误。无效符号是用于终止第一个ORDER
BY语句的分号。否则它将无法在SQL Developer中工作,但是显然当我执行通过Web App生成的同一条语句时-它将失败。


阅读 203

收藏
2021-07-01

共1个答案

admin

您需要计算方括号-合并的两个查询应放在一个方括号中-然后您的别名和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;
2021-07-01