The problem's not so much the aggregates as the views. Views containing
GROUP BY don't work properly in any but the simplest cases. Fixing this
will take a major redesign of querytrees, which we are currently hoping
to accomplish in the 7.2 development cycle.
regards, tom lane