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 24F48D1CE3E for ; Wed, 11 Feb 2004 23:46:19 +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 61717-07 for ; Wed, 11 Feb 2004 19:46:18 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id A6BCAD1B557 for ; Wed, 11 Feb 2004 19:46:16 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i1BNkG1Y021476 for ; Wed, 11 Feb 2004 23:46:16 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i1BNidKW021136 for pgsql-sql@postgresql.org; Wed, 11 Feb 2004 23:44:39 GMT From: "Greg Patnude" X-Newsgroups: comp.databases.postgresql.sql Subject: Re: determining how many products are in how many categories Date: Wed, 11 Feb 2004 15:43:37 -0800 Organization: Hub.Org Networking Services Lines: 76 Message-ID: References: <40266D61.3070304@zara.6.isreserved.com> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/162 X-Sequence-Number: 16852 You might be better off with something more like this: SELECT COUNT(P.*) AS p_count, P.p_product_category_id FROM p_product P WHERE P.p_product_category_id IN (SELECT DISTINCT id FROM p_product_category) GROUP BY P.p_product_category_id; obviously tailored to YOUR schema... not mine... -- Greg Patnude / The Digital Demention 2916 East Upper Hayden Lake Road Hayden Lake, ID 83835 (208) 762-0762 "David Garamond" wrote in message news:40266D61.3070304@zara.6.isreserved.com... > # product table (simplified): > create table p ( > id char(22) not null primary key, > name text, > desc text > ); > > # product category table (simpl.): > create table pc ( > id char(22) not null primary key, > name text, > desc text > ); > > # table that maps products into categories: > create table p_pc ( > id char(22) not null primary key, > pid char(22) not null references p(id), > pcid char(22) not null references pc(id) > ); > create index i_ppc_pid on p_pc(pid); > create index i_ppc_pcid on p_pc(pcid); > create unique index i_ppc_pid_pcid on p_pc(pid, pcid); > > There are about 50k rows in p, 40k in pc, and 140k in p_pc. Most > products are only placed in 1 category, some in 2, fewer in 3, and fewer > stills in 4, 5, 6 categories. > > I want to know how many products are placed in 1 category, how many in > 2, and so on. Here's my query: > > select count(pid),num_cat from ( > select pid,count(cid) as num_cat > from ppc group by pid) as f > group by num_cat; > > A sample output (4000 products are categorized in 5 different places, > 4998 in 4, and so on): > > count | num_cat > -------+--------- > 4000 | 5 > 4998 | 4 > 7502 | 3 > 10001 | 2 > 17499 | 1 > (5 rows) > > However, this query is taking about 2.5 minutes. Any idea on how to make > it faster? > > -- > dave > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html >