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 9E37153711 for ; Wed, 27 Apr 2005 06:13:50 -0300 (ADT) 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 90475-07 for ; Wed, 27 Apr 2005 09:13:42 +0000 (GMT) Received: from ausimss.pervasive.com (ausimss.pervasive.com [66.45.103.246]) by svr1.postgresql.org (Postfix) with ESMTP id C3AB75364F for ; Wed, 27 Apr 2005 06:13:40 -0300 (ADT) Received: from ausbayes1.aus.pervasive.com ([172.16.8.6]) by ausimss.pervasive.com with InterScan Messaging Security Suite; Wed, 27 Apr 2005 04:13:42 -0500 Received: from ausmail2k3.aus.pervasive.com ([172.16.4.8]) by ausbayes1.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Wed, 27 Apr 2005 04:13:39 -0500 Received: from BLRMAIL1.aus.pervasive.com ([10.3.16.60]) by ausmail2k3.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Wed, 27 Apr 2005 04:13:38 -0500 Subject: Re: people who buy A, also buy C, D, E Date: Tue, 26 Apr 2005 09:58:05 +0530 Message-ID: <02767D4600E59A4487233B23AEF5C5992A407F@blrmail1.aus.pervasive.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-MS-Has-Attach: Content-Transfer-Encoding: quoted-printable X-MS-TNEF-Correlator: Thread-Topic: [SQL] people who buy A, also buy C, D, E X-MimeOLE: Produced By Microsoft Exchange V6.0.6603.0 content-class: urn:content-classes:message Thread-Index: AcVKBx8CJAB3/oNWSgOCDwmPVvYByQAEIltQ From: "Ramakrishnan Muralidharan" To: "Dan Langille" , X-OriginalArrivalTime: 27 Apr 2005 09:13:39.0007 (UTC) FILETIME=[661FB4F0:01C54B09] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.227 tagged_above=0 required=5 tests=AWL, DATE_IN_PAST_24_48 X-Spam-Level: X-Archive-Number: 200504/410 X-Sequence-Number: 21303 Hi I am bit confused.. If you want to display first 5 the following query = will fetch top 5 book id's. I am not able to understand, why there is a = sub-query. SELECT ELEMENT_ID , COUNT( * ) FROM WATCH_LIST_ELEMENT GROUP BY ELEMENT_ID=20 ORDER BY COUNT(*) DESC LIMIT 5 Regards, R.Muralidharan =20 -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Dan Langille Sent: Tuesday, April 26, 2005 7:52 AM To: pgsql-sql@postgresql.org Cc: dan@langille.org Subject: [SQL] people who buy A, also buy C, D, E The goal of my query is: given a book, what did other people who=20 bought this book also buy? I plan the list the 5 most popular such=20 books. In reality, this isn't about books, but that makes it easier=20 to understand I think. We have a table of customer_id (watch_list_id) and book_id=20 (element_id). freshports.org=3D# \d watch_list_element Table "public.watch_list_element" Column | Type | Modifiers ---------------+---------+----------- watch_list_id | integer | not null element_id | integer | not null Indexes: "watch_list_element_pkey" primary key, btree (watch_list_id,=20 element_id) "watch_list_element_element_id" btree (element_id) Foreign-key constraints: "$2" FOREIGN KEY (watch_list_id) REFERENCES watch_list(id) ON=20 UPDATE CASCADE ON DELETE CASCADE "$1" FOREIGN KEY (element_id) REFERENCES element(id) ON UPDATE=20 CASCADE ON DELETE CASCADE freshports.org=3D# I have a query which returns the needed results: SELECT W.element_id FROM watch_list_element W WHERE w.watch_list_id in (select watch_list_id from=20 watch_list_element where element_id =3D 54968) GROUP BY W.element_id ORDER BY count(W.watch_list_id) DESC LIMIT 5; But performance is an issue here. So I'm planning to calculate all=20 the possible values and cache them. That is, given each element_id in=20 a watch_list, what are the top 5 element_id values on all the lists=20 on which the original element_id appears? I'm having trouble constructing the query. I'm not even sure I can=20 do this in one select, but that would be nice. Examples and clues=20 are appreciated. Any ideas? Thank you. --=20 Dan Langille : http://www.langille.org/ BSDCan - The Technical BSD Conference - http://www.bsdcan.org/ NEW brochure available at http://www.bsdcan.org/2005/advocacy/ ---------------------------(end of broadcast)--------------------------- TIP 7: don't forget to increase your free space map settings