使用SUM()两次不是最佳选择?
我知道我必须写SUM两次,如果我想在HAVING子句中使用它(否则要使用派生表): SELECT id, sum(hours) AS totalhours FROM mytable GROUP BY id HAVING sum(hours) > 50; 我现在的问题是,这是否不是最理想的。作为程序员,该查询看起来像数据库将两次计算总和。是这样,还是我应该依靠数据库引擎为我做的优化? 更新:对类似查询的解释: postgres=> explain select sum(counttodo) from orderline group by orderlineid having sum(counttodo) > 100; QUERY PLAN -------------------------------------------------------------------- HashAggregate (cost=1.31..1.54 rows=18 width=8) Filter: (sum(counttodo) > 100) -> Seq Scan on orderline (cost=0.00..1.18 rows=18 width=8) (3 …