错误:在选择中使用 AS 时,列“summed_hits”不存在 [重复]
Posted
技术标签:
【中文标题】错误:在选择中使用 AS 时,列“summed_hits”不存在 [重复]【英文标题】:ERROR: column "summed_hits" does not exist When using AS in select [duplicate] 【发布时间】:2018-05-24 12:47:36 【问题描述】:我有一个 Rails 应用程序,我正在尝试对 hstore 中的值求和。
这是查询:
# SELECT *, COALESCE(NULLIF(analytics->'2018.5.17.hits', '')::INT, 0) + COALESCE(NULLIF(analytics->'2018.5.18.hits', '')::INT, 0) + COALESCE(NULLIF(analytics->'20
18.5.19.hits', '')::INT, 0) + COALESCE(NULLIF(analytics->'2018.5.20.hits', '')::INT, 0) + COALESCE(NULLIF(analytics->'2018.5.21.hits', '')::INT, 0) + COALESCE(NULLIF(analyt
ics->'2018.5.22.hits', '')::INT, 0) + COALESCE(NULLIF(analytics->'2018.5.23.hits', '')::INT, 0) + COALESCE(NULLIF(analytics->'2018.5.24.hits', '')::INT, 0) as summed_hits F
ROM "searched_words" WHERE "searched_words"."account_id" = 2 AND (name ILIKE '%%') AND (analytics ?| ARRAY['2018.5.17.hits','2018.5.18.hits','2018.5.19.hits','2018.5.20.hit
s','2018.5.21.hits','2018.5.22.hits','2018.5.23.hits','2018.5.24.hits']) AND (summed_hits > 1) AND (analytics ?| ARRAY['2018.5.17.hits','2018.5.18.hits','2018.5.19.hits','2
018.5.20.hits','2018.5.21.hits','2018.5.22.hits','2018.5.23.hits','2018.5.24.hits']);
ERROR: column "summed_hits" does not exist
LINE 1: ...22.hits','2018.5.23.hits','2018.5.24.hits']) AND (summed_hit...
我似乎无法理解,为什么 postgres 不能从 SELECT clouse 中识别 summed_hits
...
谢谢!
【问题讨论】:
【参考方案1】:您不能在 where 中使用别名,根据:
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-SELECT-LIST
输出列的名称可用于引用该列的值 ORDER BY 和 GROUP BY 子句,但不在 WHERE 或 HAVING 子句中; 在那里你必须写出表达式。
【讨论】:
任何变通方法,所以我仍然可以实现我的功能?以上是关于错误:在选择中使用 AS 时,列“summed_hits”不存在 [重复]的主要内容,如果未能解决你的问题,请参考以下文章