如何在CASE语句中正确放置括号?

时间:2018-02-28 04:52:37

标签: sql plsql

案例陈述:

(CASE WHEN v_cur.order_type != 'STK' 
               AND v_cur.lot_net_cost_flag = 'Y' THEN 0 
      ELSE ( (v_cur.unit_costs_amount * v_cur.line_quantity_ordered) 
               / CASE WHEN v_cur.unit_of_measure_code = 'E' THEN 1 
                      WHEN v_cur.unit_of_measure_code = 'C' THEN 100 
                      WHEN v_cur.unit_of_measure_code = 'M' THEN 1000   
                      ELSE 1 END ) v_dt_unit_of_measure_code_val ) 
 END v_dt_cost_amount

2 个答案:

答案 0 :(得分:1)

case
    when v_cur.order_type != 'STK' and v_cur.lot_net_cost_flag = 'Y' then 0
    else v_cur.unit_costs_amount * v_cur.line_quantity_ordered /
        case v_cur.unit_of_measure_code
            when 'E' then 1
            when 'C' then 100
            when 'M' then 1000
            else 1
     end
end as v_dt_cost_amount

CASE语法不需要任何括号。对于混合OR / AND逻辑条件和非默认算术优先级,您只需要它们,因为(x * y) / z与上面的表达式不同x * y / z

答案 1 :(得分:0)

删除v_dt_unit_of_measure_code_val

CASE WHEN v_cur.order_type != 'STK' 
               AND v_cur.lot_net_cost_flag = 'Y' THEN 0 
     ELSE ( (v_cur.unit_costs_amount * v_cur.line_quantity_ordered) 
               / CASE WHEN v_cur.unit_of_measure_code = 'E' THEN 1 
                      WHEN v_cur.unit_of_measure_code = 'C' THEN 100 
                      WHEN v_cur.unit_of_measure_code = 'M' THEN 1000   
                      ELSE 1 END )   
 END v_dt_cost_amount