pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Madison Kelly <linux@alteeve.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Query help
Date: Thu, 08 Mar 2007 09:51:16 +0000
Message-ID: <45EFDC94.6080903@archonet.com> (raw)
In-Reply-To: <45EFAB1A.2060105@alteeve.com>
References: <45EFAB1A.2060105@alteeve.com>

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



view thread (70+ messages)  latest in thread

Message-ID: <45EFDC94.6080903@archonet.com>
Permalink:  ../45EFDC94.6080903@archonet.com/
Also on:    postgresql.org/message-id/45EFDC94.6080903@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: dev@archonet.com, linux@alteeve.com
  Subject: Re: Query help
  In-Reply-To: <45EFDC94.6080903@archonet.com>
  References: <45EFAB1A.2060105@alteeve.com> <45EFDC94.6080903@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox