Re: question re. count, group by, and having

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Rick Schumeyer" <rschumeyer(at)ieee(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: question re. count, group by, and having
Date: 2005-10-11 14:03:58
Message-ID: 13793.1129039438@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Rick Schumeyer" <rschumeyer(at)ieee(dot)org> writes:
> Is this a bug or a feature? I'm not sure why I can use 'c' in the order by
> clause but not the having clause. pg is much happier with the full "having
> count(state) > 5".

Actually, referring to any of the output columns in any of the modifier
clauses is logically suspect. Original SQL (back around 89 or so)
required ORDER BY items to be output column names, thus wiring in an
assumption that sorting happens after calculation of the output values,
but that is surely not true for any of the other clauses. And it's
pretty bogus even for sorting, since you might wish to sort on a value
you're not displaying.

If we were working in a green field we'd doubtless get rid of the
output-column-reference feature entirely. But for backward
compatibility's sake we're stuck with allowing ORDER BY items to
be simple output column names, per ancient versions of the SQL spec.
At one point or another somebody thought it a good idea to propagate
that special rule into GROUP BY; which in hindsight was an awful idea.
(It's not in the spec. I'm not sure if this is just a Postgres-ism
or if we borrowed someone else's bad idea.) But we're stuck with
supporting that odd case too, now. We certainly aren't going to add
more.

> Will this cause count to be evaluated twice?

Recent versions of PG are smart enough to merge duplicate aggregates.
This isn't necessarily true for other forms of common subexpressions,
but it works for aggregate functions.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Judith Altamirano Figueroa 2005-10-11 14:22:02 ichar
Previous Message Neil Saunders 2005-10-11 13:20:16 Difference from average