Chl*_*loe 7 mysql sql postgresql
我正在从MySQL迁移到Postgres.在MySQL中我可以使用
select sum(clicks) c from table where event_date >= '1999-01-01'
group by keyword_id
having c > 10
Run Code Online (Sandbox Code Playgroud)
Postgres给出了一个错误
错误:列"c"不存在
在Postgres中,我必须在having子句中重复该函数
select sum(clicks) c from table where event_date >= '1999-01-01'
group by keyword_id
having sum(clicks) > 10
Run Code Online (Sandbox Code Playgroud)
代码中有很多地方我必须改变.Postgres中是否有一个允许它在having子句中使用列别名的设置?
Postgres中是否有一个允许它在having子句中使用列别名的设置?
否.允许引用SELECT-list条目的实现HAVING超出标准.
您应该使用子查询,例如
select
c
from (
select
sum(clicks) c
from table
where event_date >= '1999-01-01'
group by keyword_id
) x
where c > 10;
Run Code Online (Sandbox Code Playgroud)
...或重复聚合.
您可以使用WITH查询(公用表表达式)来获得如下所示的结果
WITH t
AS (
SELECT sum(clicks) c
FROM TABLE
WHERE event_date >= '1999-01-01'
GROUP BY keyword_id
)
SELECT c
FROM t
WHERE c > 10
Run Code Online (Sandbox Code Playgroud)