简体   繁体   中英

Subquery within scalar subquery fails with error ORA-00936 Missing Expression

  1. This is the query that does not work:

     SELECT distinct ord.DateOrdered, (SELECT docno FROM th_mm_c_orderline_history WHERE th_mm_c_orderline_history_id in (SELECT max(th_mm_c_orderline_history_id) FROM th_mm_c_orderline_history GROUP BY c_orderline_id ) order by docno,c_orderline_id) as docno FROM c_order ord INNER JOIN c_orderline on c_orderline.c_order_id = ord.c_order_id INNER JOIN th_mm_c_orderline_history on th_mm_c_orderline_history.c_order_id=ord.c_order_id

    It is throwing me ORA-00936 Missing expression error

  2. This query works fine:

     SELECT docno FROM th_mm_c_orderline_history WHERE th_mm_c_orderline_history_id in (SELECT max(th_mm_c_orderline_history_id) FROM th_mm_c_orderline_history GROUP BY c_orderline_id ) order by docno,c_orderline_id as docno

Just remove the order by clause from the inline select. You can not use orde by clause there. You can use it in the outer select if you need it...This is how you can do all three of them without the error:

SELECT distinct ord.DateOrdered
       , (SELECT docno FROM th_mm_c_orderline_history 
          WHERE th_mm_c_orderline_history_id 
                 in (SELECT max(th_mm_c_orderline_history_id) 
                     FROM th_mm_c_orderline_history 
                     GROUP BY c_orderline_id) 
          ) as docno 
FROM c_order ord 
INNER JOIN c_orderline on c_orderline.c_order_id = ord.c_order_id 
INNER JOIN th_mm_c_orderline_history on th_mm_c_orderline_history.c_order_id=ord.c_order_id

You can use "order by statement" end of the whole select statement. Because of not use the column C_ORDERLINE_ID in select statement, it can be error in order by statement. Try this version in below.

 SELECT DISTINCT
       C_ORDER.DATEORDER,
       (SELECT DOCNO
          FROM TH_MM_C_ORDERLINE_HISTORY
         WHERE     C_ORDER_ID = C_ORDER_ID
               AND TH_MM_C_ORDERLINE_HISTORY_ID IN (  SELECT MAX (TH_MM_C_ORDERLINE_HISTORY_ID)
                                                        FROM TH_MM_C_ORDERLINE_HISTORY
                                                    GROUP BY C_ORDERLINE_ID)) AS DOCNO,
       C_ORDER.DOCUMENTNO
  FROM C_ORDER 
  INNER JOIN C_ORDERLINE ON C_ORDERLINE.C_ORDER_ID = C_ORDER_ID 
  ORDER BY DOCNO, C_ORDERLINE_ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM