-1

我正在尝试在 PostgreSQL 查询中使用CASE并在 where 或有条件的情况下使用它,我收到了这个错误消息

[Err] 错误:列“new_dismiss”不存在第 14 行:
trans_requistion_details.original_qty > new_dismiss

SELECT
*,
    CASE
        WHEN dismiss_qty is NULL THEN 0
        ELSE dismiss_qty
    END AS new_dismiss

FROM
 trans_requistion_details
INNER JOIN  sub_ingredients ON  trans_requistion_details.sub_ing_id =  sub_ingredients.sub_ing_id
INNER JOIN  units ON  sub_ingredients.sub_ing_stock_unit =  units.unit_id
INNER JOIN  ing_groups ON  sub_ingredients.ing_group_id =  ing_groups.ing_group_id
 HAVING
 trans_requistion_details.trans_id = 175991 and original_qty > new_dismiss
4

1 回答 1

0

尝试这个CTE

WITH j AS (
  SELECT *,
    CASE WHEN dismiss_qty is NULL THEN 0
    ELSE dismiss_qty END AS new_dismiss
FROM trans_requistion_details
) SELECT * FROM j
INNER JOIN  sub_ingredients ON  j.sub_ing_id =  sub_ingredients.sub_ing_id
INNER JOIN  units ON  sub_ingredients.sub_ing_stock_unit =  units.unit_id
INNER JOIN  ing_groups ON  sub_ingredients.ing_group_id =  ing_groups.ing_group_id
WHERE j.trans_id = 175991 and original_qty > j.new_dismiss
于 2020-01-06T13:15:10.443 回答