Received: from mail-b.bcc.ac.uk (mail-b.bcc.ac.uk [144.82.100.22]) by hub.org (8.9.3/8.9.3) with ESMTP id FAA27336 for ; Wed, 10 Nov 1999 05:59:44 -0500 (EST) (envelope-from rison@biochemistry.ucl.ac.uk) Received: from biochem.ucl.ac.uk (actually host bsmori1.biochem.ucl.ac.uk) by mail-b.bcc.ac.uk with SMTP (XT-PP) with ESMTP; Wed, 10 Nov 1999 10:58:30 +0000 Received: from bsmlx17.biochem.ucl.ac.uk (bsmlx17 [128.40.46.142]) by biochem.ucl.ac.uk (8.9.2/8.9.1) with ESMTP id KAA02094; Wed, 10 Nov 1999 10:54:27 GMT Received: from localhost (rison@localhost) by bsmlx17.biochem.ucl.ac.uk (8.9.3/8.6.6) with ESMTP id LAA06213; Wed, 10 Nov 1999 11:03:45 GMT X-Authentication-Warning: bsmlx17.biochem.ucl.ac.uk: rison owned process doing -bs Date: Wed, 10 Nov 1999 11:03:45 +0000 (GMT) From: Stuart Rison X-Sender: rison@bsmlx17 To: marten@feki.toppoint.de cc: pgsql-sql@postgreSQL.org Subject: Re: [SQL] HAVING in EXISTS-clause ... In-Reply-To: <199911091126.MAA27023@feki.phoenix-edv.netzservice.de> Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii It's just a case of reversing the order of the aggregate and the constant in your qualifier (as hinted to by the error message). This worked for me SELECT [DISTINCT] AO,AT,AV FROM P3AT AS OAT WHERE EXISTS( SELECT AO FROM P3AT WHERE AO = OAT.AO GROUP BY AO HAVING 8 = COUNT(*) ); I put the DISTINCT in square brackets to indicate that it is optional. The statement will select all rows from sets with 8 indentical AOs. Without the DISTINCT, all such instances will be returned, with the DISTINCT, only row with different combinations of AO, AT and AV are returned. Note that with the DISTINCT you have no way of indentifying which exact row has been returned from set with identical AO,AT and AV combinations. As an aside, is it worth changing the error message to something like: "ERROR: rewrite: aggregate column of view must be at rigth side in qual. Try exchanging the position of the aggregate with the value it is compared to." or should we in fact avoid such error messages? Regards, S. On Tue, 9 Nov 1999 marten@feki.toppoint.de wrote: > Hello PostgreSQL user, > > we're haveing some trouble when doing the following commands: > > a) This statement works very well ... > > SELECT AO,AT,AV FROM P3AT > GROUB BY AO > HAVING COUNT(*)>1 > > > b) This statement works very well ... > > SELECT AO,AT,AV FROM P3AT AS OAT > WHERE > EXISTS( > SELECT AO FROM P3AT > WHERE > AO = OAT.AO > GROUB BY AO) > > c) This statement does NOT work. It gives an error message: > "rewrite: aggregate column of view must be ar rigth side in qual" > > SELECT AO,AT,AV FROM P3AT AS OAT > WHERE > EXISTS( > SELECT AO FROM P3AT > WHERE > AO = OAT.AO > GROUP BY AO > HAVING COUNT(*) = 8) > > The meaning of the statement is: return the result as triples, but > make sure, that you return only these results, where you have > eight result rows available for each AO. > > Perhaps someone may help me !? > > Marten > > > > > > > ************ > Stuart C. G. Rison Department of Biochemistry and Molecular Biology 6th floor, Darwin Building, University College London (UCL) Gower Street, London, WC1E 6BT, United Kingdom Tel. 0207 504 2303, Fax. 0207 380 7193 e-mail: rison@biochem.ucl.ac.uk