不能在 having 或 where 条件中使用 case as new_c

can't use case as new_c in having or where condition

我正在尝试在 PostgreSQL 查询中使用 CASE 并在 where 或 having 条件下使用它,但我收到此错误消息

[Err] ERROR: column "new_dismiss" does not exist LINE 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

试试这个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