Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 9027E9FB49E for ; Thu, 8 Mar 2007 05:57:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 24269-09 for ; Thu, 8 Mar 2007 05:57:47 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 5B27C9FB46D for ; Thu, 8 Mar 2007 05:57:47 -0400 (AST) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 7FA5C7629A; Thu, 8 Mar 2007 09:51:17 +0000 (GMT) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 138632740CA; Thu, 8 Mar 2007 09:51:17 +0000 (GMT) Message-ID: <45EFDC94.6080903@archonet.com> Date: Thu, 08 Mar 2007 09:51:16 +0000 From: Richard Huxton User-Agent: IceDove 1.5.0.9 (X11/20061220) MIME-Version: 1.0 To: Madison Kelly Cc: pgsql-general@postgresql.org Subject: Re: Query help References: <45EFAB1A.2060105@alteeve.com> In-Reply-To: <45EFAB1A.2060105@alteeve.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.207 tagged_above=0 required=5 tests=AWL, BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200703/401 X-Sequence-Number: 110591 Madison Kelly wrote: > Hi all, > > I've got a query that looks through a table I use for my little search > engine. It's something of a reverse-index but not quite, where a proper > reverse index would have 'word | doc1, doc3, doc4, doc7' showing all the > docs the keyword is in, mine has an entry for eac > > I've got a query like: > > SELECT > sch_id, sch_for_table, sch_ref_id, sch_instances > FROM > search_index > WHERE > (sch_keyword LIKE '%digi%' OR sch_keyword LIKE '%madi%') > AND > sch_for_table!='client' > AND > ... (more restrictions) > ORDER BY > sch_instances DESC; > > This returns references to a data column (sch_ref_id) in a given table > (sch_for_table) for each matched keyword. > > The problem I am having is that two keywords might reference the same > table/column which would, in turn, give me two+ search results pointing > to the same entry. > > What I would like to do is, when two or more results match the same > 'sch_ref_id' and 'sch_for_table' to merge the results. Specifically, the > 'sch_instances' column is the number of times the given keyword is found > in the table/column. I'd like to add up the number in the duplicate > results (to give it a higher accuracy and move it up the search results). You'll want something like: SELECT sch_id, sch_for_table, sch_ref_id, SUM(sch_instances) AS tot_instances ... GROUP BY sch_id, sch_for_table, sch_ref_id ORDER BY tot_instances DESC; The key word to search the manuals on is "aggregates" (sum(), count() etc). -- Richard Huxton Archonet Ltd