sql case语句语法错误

时间:2011-09-14 07:50:32

标签: sql

SELECT ph.order_ref_no,
       pd.porder_item_no,
       pd.catlog_code,
       pd.order_qty,
       pd.part_number,
       ph.porder_no,
       ph.order_date,
       pd.date_required,
       pd.project_no,
       s.name,
       SUM(pd.order_qty * pd.price) -
       ( SUM(pd.order_qty * pd.price) * pd.discount_pct ) + ( SUM(
       pd.order_qty * pd.price) * pd.gst_rate ) AS total,
       CASE ph.status
         WHEN ( pd.order_qty > SUM(dd.recd_qty) ) THEN 'Proceed'
         WHEN pd.order_qty = SUM(dd.recd_qty) THEN 'Completed'
         WHEN 'X' THEN 'canceled'
         ELSE 'New'
       END                                      AS status
FROM   zpu_purch_heade ph
       INNER JOIN zpu_purch_detai pd
         ON ph.porder_no = pd.porder_no
       INNER JOIN zpu_supplie s
         ON ph.supplier_code = s.supplier_code
       LEFT JOIN zpu_deldoc_detai dd
         ON ph.porder_no = dd.porder_no
WHERE  ph.client_code = 'METTRA'
GROUP  BY pd.porder_item_no,
          ph.order_ref_no,
          pd.project_no,
          ph.porder_no,
          ph.order_date,
          pd.date_required,
          pd.order_qty,
          pd.part_number,
          pd.price,
          pd.discount_pct,
          pd.gst_rate,
          pd.status,
          s.name,
          ph.status,
          pd.catlog_code,
          pd.order_qty
ORDER  BY ph.order_date DESC  

我收到错误“'>'附近的语法错误。”如果声明中有错误吗?

3 个答案:

答案 0 :(得分:4)

要修复语法错误,您需要将case语句更改为:

CASE 
  WHEN ( pd.order_qty > SUM(dd.recd_qty) ) THEN 'Proceed'
  WHEN pd.order_qty = SUM(dd.recd_qty) THEN 'Completed'
  WHEN ph.status = 'X' THEN 'canceled'
  ELSE 'New'
END AS status

答案 1 :(得分:0)

问题在于这一部分:

CASE ph.status  WHEN (pd.order_qty > sum(dd.recd_qty))

您不能包含SUM这样的内容。

答案 2 :(得分:0)

ph.status不正确后

CASE。删除它。