在Postgres中的hading子句中引用选择聚合列别名 [英] Referring to a select aggregate column alias in the having clause in Postgres
问题描述
我正在从MySQL迁移到Postgres.在MySQL中,我可以使用
I'm migrating from MySQL to Postgres. In MySQL I can use
select sum(clicks) c from table where event_date >= '1999-01-01'
group by keyword_id
having c > 10
Postgres给出了错误
Postgres gives an error
错误:列"c"不存在
ERROR: column "c" does not exist
在Postgres中,我必须在hading子句中重复该功能
In Postgres I have to repeat the function in the having clause
select sum(clicks) c from table where event_date >= '1999-01-01'
group by keyword_id
having sum(clicks) > 10
我必须在代码中更改很多地方.在Postgres中是否有一个设置可以允许它在hading子句中使用列别名?
There are a lot of places in the code that I have to change. Is there a setting in Postgres that will allow it to use column aliases in the having clause?
推荐答案
Postgres中是否有一个设置,允许它在hading子句中使用列别名?
Is there a setting in Postgres that will allow it to use column aliases in the having clause?
不.允许引用HAVING
中的SELECT
-list条目的实现超出了标准.
No. Implementations that allow references to SELECT
-list entries in HAVING
are going outside the standard.
您应该使用子查询,例如
You should use a subquery, e.g.
select
c
from (
select
sum(clicks) c
from table
where event_date >= '1999-01-01'
group by keyword_id
) x
where c > 10;
...或重复汇总.
这篇关于在Postgres中的hading子句中引用选择聚合列别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!