无法将”case”用作HAVING或WHERE条件中的”new_c”。

huangapple go评论72阅读模式
英文:

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

问题

我正在尝试在PostgreSQL查询中使用CASE,并将其用于where或having条件,并收到以下错误消息:

[Err] ERROR: 列 "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
英文:

I'm trying to use CASE in PostgreSQL query and use it in where or having condition and i got this error message

> [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

答案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
英文:

Try this 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

huangapple
  • 本文由 发表于 2020年1月6日 21:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定