X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 33CCD329E8C for ; Mon, 20 Sep 2004 20:53:30 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 90702-03 for ; Mon, 20 Sep 2004 19:53:22 +0000 (GMT) Received: from msgdirector2.onetel.net.uk (msgdirector2.onetel.net.uk [212.67.96.149]) by svr1.postgresql.org (Postfix) with ESMTP id D50BA329E8E for ; Mon, 20 Sep 2004 20:53:22 +0100 (BST) Received: from bolek.coachhouse (213-78-120-117.friaco.onetel.net.uk [213.78.120.117]) by msgdirector2.onetel.net.uk (MOS 3.4.6-GR) with ESMTP id BWR81549; Mon, 20 Sep 2004 20:53:11 +0100 (BST) Received: (Exim 3.36) #1 (Debian)) protocol: esmtp id 1C9UBG-0005lX-00 ; Mon, 20 Sep 2004 20:50:34 +0100 Message-ID: <414F348A.2020608@numerixtechnology.de> Date: Mon, 20 Sep 2004 20:50:34 +0100 From: T E Schmitz Reply-To: mailreg@numerixtechnology.de User-Agent: Mozilla Thunderbird 0.7 (Windows/20040616) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Greg Stark , pgsql-sql@postgresql.org Subject: Re: COUNT(*) to find records which have a certain number of References: <414EF3E6.9040202@numerixtechnology.de> <87llf4hjv2.fsf@stark.xeocode.com> In-Reply-To: <87llf4hjv2.fsf@stark.xeocode.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/167 X-Sequence-Number: 19013 Hello Greg, You have given me plenty of food for thought. Thank you for taking the time. Currently, the tables have such few records (350, 900, 1000) that performance does not come into it, particularly seeing as this was only needed for a one-shot report. However, I have stached your examples away for future reference. I was feeling a bit guilty about posting such a trivial question. I can cobble together some straightforward SQL but I could really do with a source of more complex SQL examples. If you know of any links - that would great and save the list from more such questions ;-) I am correcting a couple of typos below in case someone tries these examples out. Greg Stark wrote: > select * > from brand > join model on (brand_pk = brand_fk) > where (select count(*) > from type > where model_fk = model_pk > having sum(case when type = 'xyz' then 1 else 0 end) >= 1 > ) > 1 ... having sum(case when type_name = 'xyz' ... > select * > from brand > join model on (brand_pk = brand_fk) > join (select model_fk > from type > group by model_fk > having sum(case when type = 'xyz' then 1 else 0 end) >= 1 > and count(*) > 1 > ) on (model_fk = model_pk) > ) as somealias on (model_fk = model_pk) (subquery in FROM must have an alias) -- Regards/Gruß, Tarlika