Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 578BF771F74 for ; Sun, 20 May 2012 00:31:59 -0300 (ADT) Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SVws3-0007KT-Px for pgsql-sql@postgresql.org; Sun, 20 May 2012 03:31:58 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1SVwro-0007Ge-1D for pgsql-sql@postgresql.org; Sun, 20 May 2012 05:31:36 +0200 Received: from ip-118-90-75-239.xdsl.xnet.co.nz ([118.90.75.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 20 May 2012 05:31:35 +0200 Received: from jasen by ip-118-90-75-239.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 20 May 2012 05:31:35 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: Need help in grouping records Date: 20 May 2012 03:04:12 GMT Organization: Dis (not Dat) Organisation Lines: 46 Message-ID: References: <4FB7EEC6.1030004@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-75-239.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/53 X-Sequence-Number: 36601 On 2012-05-19, Andreas wrote: > Hi, > > I'm trying to fight against double entries in tables. > I got as far as I can find similar records with trigram string matching. > If I do this with a table compared to itself I get something like this: > > id_a, id_b > 3, 5 > 3, 7 > 5, 3 > 5, 7 > 7, 3 > 7, 5 > 11, 13 > 13, 11 > > so the records with the IDs 3, 5, 7 and 11, 13 are similar enough to > form a group. > > How would I get a list of record-IDs with a group-ID like this > > record_id, group_id > 3, 1 > 5, 1 > 7, 1 > 11, 2 > 13, 2 > > Is there a way to get this by SQL ? select id_a,min(least(id_a,id_b)) from SOMETHING group by id_a close enough? or this: ? select id_a, rank() over order by g from ( select id_a,min(least(id_a,id_b)) as g from SOMETHING group by id_a ) as foo -- ⚂⚃ 100% natural