rikard@ngs.hr writes:
> select case when doSlow then sum(slowFunction()) else sum(1) end as total
> from something group by doSlow
You are misunderstanding how aggregates work. The aggregates are
evaluated first, then the surrounding expressions are done once at
the end of the query (or group). The fact that the CASE might choose
the other branch at the end doesn't eliminate the necessity to run
both aggregates across all the rows.
regards, tom lane