Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 0CA5A1C07888 for ; Mon, 7 Nov 2011 17:52:38 -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 45258-01-2 for ; Mon, 7 Nov 2011 21:52:32 +0000 (UTC) Received: from mx1.hub.org (mx1.hub.org [200.46.208.106]) by mail.postgresql.org (Postfix) with ESMTP id EC575D6B9AB for ; Mon, 7 Nov 2011 17:50:18 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by mx1.hub.org (Postfix) with ESMTP id CE94A17A3DA0 for ; Mon, 7 Nov 2011 17:48:59 -0400 (AST) Received: from [88.210.164.29] (helo=lolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1RNX3U-0003nO-Ha for pgsql-sql@postgresql.org; Mon, 07 Nov 2011 21:48:38 +0000 Received: from dick.coachhouse ([192.168.1.32] ident=tarlika) by lolek.coachhouse with esmtp (Exim 4.72) (envelope-from ) id 1RNWcK-0007aq-H6 for pgsql-sql@postgresql.org; Mon, 07 Nov 2011 21:20:32 +0000 Date: Mon, 7 Nov 2011 21:20:31 +0000 From: Tarlika Elisabeth Schmitz To: pgsql-sql@postgresql.org Subject: GROUP and ORDER BY Message-ID: <20111107212031.4d6310aa@dick.coachhouse> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.20.1; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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/28 X-Sequence-Number: 36001 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? -- Best Regards, Tarlika Elisabeth Schmitz