X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 27CBA4763DF for ; Mon, 5 May 2003 16:03:24 -0400 (EDT) Received: from taka.swcp.com (taka.swcp.com [198.59.115.12]) by postgresql.org (Postfix) with ESMTP id DF91D4763B5 for ; Mon, 5 May 2003 16:03:20 -0400 (EDT) Received: from ARCplanning.com (pcp02742825pcs.sntafe01.nm.comcast.net [68.35.10.62]) by taka.swcp.com (8.12.9/8.12.9) with ESMTP id h45K3J7Y047822; Mon, 5 May 2003 14:03:20 -0600 (MDT) Date: Mon, 5 May 2003 14:03:18 -0600 Subject: Re: 2 simple SQL questions: optimizing aggegate query Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v552) Cc: pgsql-sql@postgresql.org To: Bruno Wolff III From: Alex Rice In-Reply-To: <20030505194518.GA5200@wolff.to> Message-Id: <9D0F6008-7F34-11D7-977E-000393529642@ARCplanning.com> Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.552) X-Spam-Status: No, hits=-28.3 required=5.0 tests=BAYES_10,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES,USER_AGENT_APPLEMAIL autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/67 X-Sequence-Number: 13232 On Monday, May 5, 2003, at 01:45 PM, Bruno Wolff III wrote: >> SELECT url.rec_id, url, title, content_type, txt, >> sum( >> case >> when dict.word = 'wordx' then 1 >> when dict.word = 'wordx' then 1 >> when dict.word = 'wordy' then 1 >> else 0 >> end >> ) as rank >> FROM dict, url >> WHERE url.rec_id = dict.url_id >> GROUP BY rec_id, url, title, content_type, txt >> ORDER BY rank DESC > > Is there some reason you can't put the word list in the where clause? > (You can use coalesce to set NULL sums to 0.) Can you give an example of what you mean use coalesce and how to use it to produce a count of the matched words, the rank? I am converting some existing query examples that use the form SUM( dict.word IN ( word list...)) which doesn't work with pgsql. I got the idea for the case statement from this archive message http://archives.postgresql.org/pgsql-sql/1999-10/msg00136.php Thanks much, Alex Rice, Software Developer Architectural Research Consultants, Inc. alrice@ARCplanning.com alrice@swcp.com