PostgreSQL - 别名列和 HAVING
SELECT
CASE WHEN SUM(X.Count)*3600 is null THEN '0'
ELSE
SUM(X.Count)*3600
END AS PJZ,
X.Mass
FROM X
WHERE X.Mass > 2000
HAVING ((X.Mass / PJZ * 100) - 100) >= 10;
获取:错误:列“pjz”不存在。
我怎样才能做这样的事情?
SELECT
CASE WHEN SUM(X.Count)*3600 is null THEN '0'
ELSE
SUM(X.Count)*3600
END AS PJZ,
X.Mass
FROM X
WHERE X.Mass > 2000
HAVING ((X.Mass / PJZ * 100) - 100) >= 10;
Getting: ERROR: Column »pjz« doesn't exists.
How can I do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能在having 中使用别名,并且必须在having 语句中重复该语句。由于您只想检查 null,因此可以这样做:
You can't use aliases in a having, and have to duplicate the statement in the having cluause. Since you only want to check for null, you could do this:
其他选项是用
WITH
语句包围查询 - 例如:在我看来,它比代码重复要好得多
Other option is to surround query by
WITH
statement - for example:It is far better then code duplication in my opinion
将其包装到派生表中:(
请注意,我添加了缺少的组,否则查询将无效)
Wrap it into a derived table:
(Note that I added the missing group by as otherwise the query would not be valid)