Re: Problem With Case Statement and Aggregate Functions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Shea <andrew(at)octahedron(dot)com(dot)au>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Problem With Case Statement and Aggregate Functions
Date: 2007-05-11 13:01:21
Message-ID: 17423.1178888481@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Andrew Shea <andrew(at)octahedron(dot)com(dot)au> writes:
> However the following code doesn't work even though it is very similar
> to the first query (that is, and aggregate function within a case
> statement):

> select (SELECT CASE WHEN (1=2) THEN 0 ELSE COUNT(*) END) from (
^^^^^^
> select 1 as count union select 2 union select 3
> ) as "temp";

Lose the underlined SELECT and it will behave the way you expect.
As-is the COUNT is an aggregate of that sub-select, not of the topmost
select. To be considered an aggregate of the topmost select it has
to reference a variable of that query level.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Bojan Jovanovic 2007-05-11 13:06:02 BUG #3269: PSQL does not display error output
Previous Message Nilay Ceter 2007-05-11 09:36:49 BUG #3268: pgpass.conf error