Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7CEA71A3E38A for ; Wed, 8 Feb 2012 09:50:55 -0400 (AST) Received: from mahout.postgresql.org ([98.129.198.123]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rv7vC-0001fG-Di for pgsql-bugs@postgresql.org; Wed, 08 Feb 2012 13:50:55 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rv7uz-0005y7-75 for pgsql-bugs@postgresql.org; Wed, 08 Feb 2012 13:50:42 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1Rv7uy-0002pB-Vf for pgsql-bugs@postgresql.org; Wed, 08 Feb 2012 13:50:40 +0000 Date: Wed, 08 Feb 2012 13:50:40 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #6440: Window function in WHERE evaluated after agregate To: pgsql-bugs@postgresql.org From: logik@centrum.cz X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/77 X-Sequence-Number: 32420 The following bug has been logged on the website: Bug reference: 6440 Logged by: Matyas Novak Email address: logik@centrum.cz PostgreSQL version: 9.1.1 Operating system: Linux Description: = Window function in WHERE clause - as all other functions used in where - should be evaluated before agregating takes place. But it seems that postgresql try evaluate its after agregating, as it'd be in HAVING clause. E.g. in folowing example I'l try to sum the best results of given persons from all dispciplines - so I agregate over persons and best result in each discipline wann get using window function. (I'm aware that it can be done by various subselects, or that there may be better to use different window function or nested agregates, but it's simplified example from a bit complex example.) Test example: create table results ( id serial not null primary key, person integer, discipline integer, result integer ); select person, sum(result) from results = where row_number() over (partition by person, discipline order by result desc) = =3D 1 group by person Error: ERROR: column "results.result" must appear in the GROUP BY clause or be used in an aggregate function LINE 19: ...() over (partition by person, discipline order by result des...