Re: Alias in the HAVING clause

From: "David Wilson" <david(dot)t(dot)wilson(at)gmail(dot)com>
To: "Nathan Thatcher" <n8thatcher(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Alias in the HAVING clause
Date: 2008-05-13 23:01:12
Message-ID: e7f9235d0805131601x25817293o75bcfde136d9b569@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, May 13, 2008 at 5:43 PM, Nathan Thatcher <n8thatcher(at)gmail(dot)com> wrote:
> Is this the correct way to do this, or is there a better way / a way
> to get PostgreSQL to recognize an alias in the HAVING clause?

As Tom pointed out, f1's not in scope for the HAVING clause. If you're
that concerned about expression duplication, you could move the
calculation into a sub-select:

SELECT COUNT(*), f1 FROM (SELECT id % 3 AS f1 FROM table) t1 GROUP BY
f1 HAVING f1 <> 0;

--
- David T. Wilson
david(dot)t(dot)wilson(at)gmail(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-05-13 23:11:01 Re: Making sure \timing is on
Previous Message Tom Lane 2008-05-13 22:43:25 Re: Alias in the HAVING clause