简体   繁体   中英

Order BY in Subquery : Missing right parenthese

I try to update 2 columns using a subquery with a specific order so the query is like that:

UPDATE myTable TAO
  SET (TAO.BASE_AMT,TAO.TAX_CD_VAT_PCT) = (SELECT SUM(decode(TAX_CD_VAT_PCT,0,0,d.monetary_amount))
                                                , MAX (D.TAX_CD_VAT_PCT)
                                           FROM source1 w
                                              , source2 d
                                           WHERE 1=1
                                           -- Some conditions
                                            GROUP BY d.business_unit, d.voucher_id
                                            ORDER BY d.business_unit, d.voucher_id);

Why am I getting:

ORA-00907: missing right parenthesis

EDIT 1

the query with all conditions :

UPDATE myTable TAO 
  SET (TAO.BASE_AMT,TAO.TAX_CD_VAT_PCT) = ( 
 SELECT SUM(decode(TAX_CD_VAT_PCT 
 , 0 
 , 0 
 ,d.monetary_amount)) 
 , MAX (D.TAX_CD_VAT_PCT) 
  FROM source1 w 
  , source2 d 
 WHERE w.business_unit = TAO.BUSINESS_UNIT 
   AND d.business_unit = w.business_unit 
   AND d.voucher_id = w.voucher_id 
   AND d.VOUCHER_LINE_NUM = w.VOUCHER_LINE_NUM 
   AND d.VOUCHER_ID = TAO.VOUCHER_ID 
  GROUP BY d.business_unit, d.voucher_id
  ORDER BY d.business_unit, d.voucher_id )

Subquery have group by clause which can return multiple rows. Update query cannot take list of values to update.

So, for every unique combination of d.business_unit, d.voucher_id , there will be one row even if you did not select those columns in the inner query.

Also, assuming it returns only one row, you are missing where clause for update query. Only where clause I can see is in subquery.

You would appear to want a correlated subquery, because your subquery can return multiple rows.

You don't provide a table layout, but I would expect:

UPDATE myTable TAO
  SET (BASE_AMT, TAX_CD_VAT_PCT) =
          (SELECT SUM(d.monetary_amount),  -- the DECODE does nothing
                  MAX(D.TAX_CD_VAT_PCT)
           FROM source1 w JOIN  -- ALWAYS use proper, explicit, standard JOIN syntax
                source2 d
                ON . . . 
           WHERE . . . AND
                 d.business_unit = TAO.business_unit AND
                 d.voucher_id = TAO.voucher_id
          );

Your code is filled with anachronisms and bad practices:

  • The DECODE() does nothing. If it did, you should use CASE expressions instead.
  • Commas in JOIN clauses have been obsolete for over fifteen years. Use proper, explicit, standard JOIN syntax.
  • The ORDER BY in the subquery does nothing.

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