我构建了这个SQL查询,它搜索所有策略号并返回所有多个PPOLNO货币值的总和:
SELECT PPOLNO, SUM(PPRMPD) AS SUM
FROM PFNTLPYMTH
WHERE SUM(PPRMPD) >= 5000
AND ((PYEAR=2012 AND PMONTH >=3
AND PDAY >=27) OR (PYEAR=2013
AND PYEAR <=3 AND PDAY<=27))
GROUP BY PPOLNO
我正在做的只是在SUM&gt; =特定值时返回它们。这可能吗?怎么样?我试过这个:
SELECT PPOLNO, SUM(PPRMPD) AS SUM FROM PFNTLPYMTH
WHERE SUM(PPRMPD) >= 5000 AND ((PYEAR=2012 AND PMONTH >=3 AND PDAY >=27) OR (PYEAR=2013
AND PYEAR <=3 AND PDAY<=27)) GROUP BY PPOLNO
但无济于事。我也尝试过输入WHERE SUM&gt; = 5000,但是再一次没有。
非常感谢任何帮助。
答案 0 :(得分:18)
每当您需要对聚合(SUM
是)执行“WHERE”子句时,您需要使用HAVING
子句。
SELECT PPOLNO, SUM(PPRMPD) AS SUM FROM PFNTLPYMTH
WHERE ((PYEAR=2012 AND PMONTH >=3 AND PDAY >=27) OR (PYEAR=2013
AND PYEAR <=3 AND PDAY<=27)) GROUP BY PPOLNO
HAVING SUM(PPRMPD) >= 5000
答案 1 :(得分:2)
在聚合之前应用<{1}}子句中的谓词。要在聚合之后应用谓词(过滤器),请使用Where
子句。 ..试试
Having