X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id D312AD1D554 for ; Fri, 12 Mar 2004 13:44:45 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 17848-02 for ; Fri, 12 Mar 2004 09:44:46 -0400 (AST) Received: from localhost.localdomain (unknown [200.245.23.5]) by svr1.postgresql.org (Postfix) with ESMTP id D114AD1D17D for ; Fri, 12 Mar 2004 09:44:40 -0400 (AST) Received: from yahoo.com.br (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.12.8/8.12.8) with ESMTP id i2CDsQLb002074; Fri, 12 Mar 2004 10:54:50 -0300 Message-ID: <4051C111.40301@yahoo.com.br> Date: Fri, 12 Mar 2004 10:54:25 -0300 From: Daniel Henrique Alves Lima Reply-To: email_daniel_h@yahoo.com.br User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Gary Stainburn Cc: pgsql-sql@postgresql.org Subject: Re: where not unique References: <200403121302.09243.gary.stainburn@ringways.co.uk> In-Reply-To: <200403121302.09243.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/145 X-Sequence-Number: 17244 Gary Stainburn wrote: >Hi folks, > >I've got a table which contains amongst other things a stock number and a >registration number. I need to print out a list of stock number and reg >number where reg number is not unique (cherished number plate transfer not >completed). I've tried variations of a theme based on > >select stock_number, registration from stock where registration in > (select registration, count(registration) as count > from stock where count > 1 group by registration); > >but I have two problems. Firstly with the sub-select I get: > >usedcars=# select registration, count(registration) as count from stock where >count > 1 group by registration; >ERROR: Attribute 'count' not found >usedcars=# > >although if I miss out the where clause I get the expected results. > I think that this is not the best way, but : select * from (select registration, count(registration) as counter from stock group by registration) where counter > 1; > >Secondly, when I run the full query I get: > >usedcars=# select stock_number, registration from stock >usedcars-# where registration in >usedcars-# (select registration, count(registration) as count from stock group >by registration); >ERROR: Subselect has too many fields >usedcars=# > > This is because the subselect is returning 2 columns but "in clause" is expecting just one column. Try to use "exists" instead of "in", ok ? >which is obviously because of the count field. > >Can anyone tell me where I'm going wroing with these count fields? >(I've tried renaming the field to regcount in case it was a reserved word >problem). > > I hope this helps you.