Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 8E9A9183A970 for ; Tue, 8 Nov 2011 00:27:21 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 66800-07 for ; Tue, 8 Nov 2011 04:27:15 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mmmail1.mcr.colo.comodoca.net (mdfw.comodoca.net [91.209.196.68]) by mail.postgresql.org (Postfix) with ESMTP id 0DB23183A971 for ; Tue, 8 Nov 2011 00:27:13 -0400 (AST) Received: (qmail 28473 invoked from network); 8 Nov 2011 04:27:11 -0000 Received: from ian.bd.office.comodo.net (HELO ian.brad.office.comodo.net) (192.168.0.201) by mail.colo.comodoca.net with ESMTPS (DHE-RSA-AES256-SHA encrypted); 8 Nov 2011 04:27:11 -0000 Received: (qmail 14579 invoked by uid 1000); 8 Nov 2011 04:27:11 -0000 Received: from Unknown (HELO [192.168.199.58]) (192.168.199.58) (smtp-auth username robinst, mechanism plain) by ian.brad.office.comodo.net (qpsmtpd/0.40) with (CAMELLIA256-SHA encrypted) ESMTPSA; Tue, 08 Nov 2011 04:27:11 +0000 Message-ID: <4EB8AF9C.7060305@comodo.com> Date: Tue, 08 Nov 2011 09:57:08 +0530 From: Robins Tharakan Organization: Comodo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 MIME-Version: 1.0 To: pgsql-sql@postgresql.org CC: Tarlika Elisabeth Schmitz Subject: Re: GROUP and ORDER BY References: <20111107212031.4d6310aa@dick.coachhouse> In-Reply-To: <20111107212031.4d6310aa@dick.coachhouse> 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.001 tagged_above=-5 required=5 tests=BAYES_20=-0.001 X-Spam-Level: X-Archive-Number: 201111/29 X-Sequence-Number: 36002 Unless I overlooked something here, does this work ? SELECT no, name, MAX(similarity(name, 'Tooneyvara')) AS sim FROM vtown WHERE similarity(name, 'Tooneyvara') > 0.4 GROUP BY no, name ORDER BY sim DESC -- Robins Tharakan On 11/08/2011 02:50 AM, Tarlika Elisabeth Schmitz wrote: > Hello, > > I would like to GROUP the result by one column and ORDER it by another: > > SELECT > no, name, similarity(name, 'Tooneyvara') AS s > FROM vtown > WHERE similarity(name, 'Tooneyvara')> 0.4 > ORDER BY s DESC > > Result: > > 1787 "Toomyvara" 0.5 > 1787 "Toomevara" 0.4 > 1188 "Toonybara" 0.4 > > > Desired result: > > 1787 "Toomyvara" 0.5 > 1188 "Toonybara" 0.4 > > Gets rid of the duplicate "no" keeping the spelling with the greater > similarity and presents the remaining result ordered by similarity. > > > My solution: > > SELECT * FROM > ( > SELECT DISTINCT ON (no) > no, name, > similarity(name, 'Tooneyvara') AS sim > FROM vtown > WHERE similarity(name, 'Tooneyvara')> 0.4 > ORDER BY no, sim DESC > ) AS x > ORDER BY sim > > > Is that the best way to achieve this result?