ERROR: column "summed_hits" does not exist When using AS in select
ERROR: column "summed_hits" does not exist When using AS in select
我有一个 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 语句中识别出 summed_hits
...
谢谢!
您不能在 where 中使用别名,根据:
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-SELECT-LIST
An output column's name can be used to refer to the column's value in
ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses;
there you must write out the expression instead.
我有一个 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 语句中识别出 summed_hits
...
谢谢!
您不能在 where 中使用别名,根据:
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-SELECT-LIST
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.