aliases, &c in HAVING clause?

From: david(at)fetter(dot)org (David Fetter)
To: pgsql-hackers(at)postgresql(dot)org
Subject: aliases, &c in HAVING clause?
Date: 2004-02-23 19:42:02
Message-ID: NmGdnTtETvgXyqfd3czS-w@speakeasy.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Kind people,

I bumped across this several times, and am wondering what SQL99 and
SQL200x have to say about column numbers or aliases in HAVING. SQL92
is fairly clear (no) but also somewhat out of date.

Here's a scenario:

SELECT foo_name, count(*) as foo_count
FROM foo
GROUP BY foo_name
HAVING foo_count > 2
ORDER BY foo_count DESC, foo_name;

Another:

SELECT foo_name, count(*) as foo_count
FROM foo
GROUP BY foo_name
HAVING 2 > 2 -- OK, so this is a little weird, but it makes sense in context.
ORDER BY 2 DESC, 1;

I know pg throws some kind of parse error when I ask for that sort of
thing, and that replacing the alias/number with its referent clears
this up, but what do later standards have to say about doing or not
doing the above? If they're ambiguous, are there good reasons why the
above shouldn't work?

TIA for any pointers on this :)

Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Transported to a surreal landscape, a young girl kills the first
woman she meets and then teams up with three complete strangers
to kill again.
Marin County newspaper's TV listing for The Wizard of Oz

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Hallgren 2004-02-23 22:11:44 Re: Pl/Java - next step?
Previous Message David Mosberger 2004-02-23 18:18:33 dbt-2 tests & profiling on ia64