此查询(最小可重现的示例):
WITH t as ( SELECT 3 id,2 price,0 amount ) SELECT CASE WHEN amount > 0 THEN SUM(price / amount) ELSE price END u_price FROM t GROUP BY id,price,amount
在Postgresql 9.4抛出
division by zero
没有SUM就可以了.
这怎么可能?
我喜欢这个问题,我求助于这些
tough guys:
策划者有罪:
A CASE cannot prevent evaluation of an aggregate expression contained
within it,because aggregate expressions are computed before other
expressions in a SELECT list or HAVING clause are considered
更多细节见https://www.postgresql.org/docs/10/static/sql-expressions.html#SYNTAX-EXPRESS-EVAL