sqloracle-databaseora-00936

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
    

Solution

  • 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