Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 7BF03475F00 for ; Fri, 4 Oct 2002 17:05:11 -0400 (EDT) Received: from chico.benjamindsmith.com (adsl-63-195-17-22.dsl.chic01.pacbell.net [63.195.17.22]) by postgresql.org (Postfix) with ESMTP id 9538A475C9E for ; Fri, 4 Oct 2002 17:05:10 -0400 (EDT) Received: from localhost (chico.benjamindsmith.com [63.195.17.22]) by chico.benjamindsmith.com (8.11.6/8.11.6) with SMTP id g94L5BL25197 for ; Fri, 4 Oct 2002 14:05:12 -0700 From: bens_nospam@benjamindsmith.com Message-Id: <200210042105.g94L5BL25197@chico.benjamindsmith.com> To: Subject: Self Join? Date: Fri, 4 Oct 2002 14:05:11 PDT Reply-To: Errors-To: X-Priority: 3 (Normal) X-Originating-Ip: [10.254.254.10] X-Mailer: NOCC v0.9.5 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/69 X-Sequence-Number: 9787 I'm having difficulty coming up with the right join to get my results. I'm using PostgreSQL 7.2.x I'm looking for a "most likely match" result. Assume you have a table with two fields, field 1 is a serial key (unique) and field 2 is varchar. Assume that you have the following entries in the table: recordid val 1, 'a' 2, 'b' 3, 'ab' And I want to match the strings "a" and "b", but not necessarily "ab", and disregard an additional "c", and organize the result so that the records that best match are at the top. Sample output might be: count recordid 2 3 1 1 1 2 Record #3, containing both "a" and "b" has two count, records 1 and 2 having only one of "a" or "b" have a count of 1. The closest that I've come so far is from a query like select id from table where lower(val) like lower('%a%') UNION ALL select id from table where lower(val) like lower('%b%') UNION ALL select id from table where lower(val) like lower(%c%'); What this gives me is id 1 3 2 3 which is somewhat close, but then requires me to loop thru a potentially large number of results to get the requested output. Anybody else up to this one? -Ben