PostgreSQL-Aliases 列及 HAVING[英] PostgreSQL - Aliases column and HAVING

本文是小编为大家收集整理的关于PostgreSQL-Aliases 列及 HAVING的处理方法,想解了PostgreSQL-Aliases 列及 HAVING的问题怎么解决?PostgreSQL-Aliases 列及 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 PJZ = 0 THEN 100
          ELSE PJZ
       END as PJZ,
       mass
FROM (
    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  
    GROUP BY X.mass
) t
WHERE PJZ = 0 
   OR ((X.Mass / PJZ * 100) - 100) >= 10;

(请注意,我添加了缺少的组,否则查询将无效)

本文地址:https://www.itbaoku.cn/post/1763776.html