Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id B2AB1632AA4 for ; Wed, 11 Aug 2010 12:54:31 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 88372-05-6 for ; Wed, 11 Aug 2010 15:54:24 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from spinlock.commandprompt.com (host-215.commandprompt.net [207.173.203.215]) by mail.postgresql.org (Postfix) with ESMTP id 00483633D68 for ; Wed, 11 Aug 2010 12:54:23 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by spinlock.commandprompt.com (Postfix) with ESMTP id 03A07348040; Wed, 11 Aug 2010 08:54:22 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at commandprompt.com Received: from spinlock.commandprompt.com ([127.0.0.1]) by localhost (spinlock.commandprompt.com.commandprompt.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ApjILPlRNCsu; Wed, 11 Aug 2010 08:54:19 -0700 (PDT) Received: from perhan.alvh.no-ip.org (190-95-29-207.bk17-dsl.surnet.cl [190.95.29.207]) by spinlock.commandprompt.com (Postfix) with ESMTPSA id 62FA734803D; Wed, 11 Aug 2010 08:54:19 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 49A456E619; Wed, 11 Aug 2010 11:54:17 -0400 (CLT) Content-Type: text/plain; charset=UTF-8 Cc: pgsql-performance Subject: Re: Sorted group by From: Alvaro Herrera To: Matthew Wakeling In-reply-to: References: Date: Wed, 11 Aug 2010 11:54:17 -0400 Message-Id: <1281541760-sup-2001@alvh.no-ip.org> User-Agent: Sup/git Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201008/129 X-Sequence-Number: 39972 Excerpts from Matthew Wakeling's message of mar ago 10 11:40:16 -0400 2010: > I am trying to retrieve, for many sets of rows grouped on a couple of > fields, the value of an ungrouped field where the row has the highest > value in another ungrouped field. I think this does what you want (schema is from the tenk1 table in the regression database): select string4 as group, (array_agg(stringu1 order by unique1 desc))[1] as value from tenk1 group by 1 ; Please let me know how it performs with your data. The plan is rather simple: regression=# explain analyze select string4 as group, (array_agg(stringu1 order by unique1 desc))[1] as value from tenk1 group by 1 ; QUERY PLAN ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── GroupAggregate (cost=0.00..1685.16 rows=4 width=132) (actual time=22.825..88.922 rows=4 loops=1) -> Index Scan using ts4 on tenk1 (cost=0.00..1635.11 rows=10000 width=132) (actual time=0.135..33.188 rows=10000 loops=1) Total runtime: 89.348 ms (3 filas) -- Álvaro Herrera The PostgreSQL Company - Command Prompt, Inc. PostgreSQL Replication, Consulting, Custom Development, 24x7 support