Sorted group by - Mailing list pgsql-performance

From Matthew Wakeling
Subject Sorted group by
Date
Msg-id alpine.DEB.2.00.1008101627440.2654@aragorn.flymine.org
Whole thread Raw
Responses Re: Sorted group by  (Thomas Kellerer <spam_eater@gmx.net>)
Re: Sorted group by  (hubert depesz lubaczewski <depesz@depesz.com>)
Re: Sorted group by  ("Kevin Grittner" <Kevin.Grittner@wicourts.gov>)
Re: Sorted group by  (Alvaro Herrera <alvherre@commandprompt.com>)
List pgsql-performance
I'm trying to eke a little bit more performance out of an application, and
I was wondering if there was a better way to do the following:

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. For instance, I have the following table
setup:

group  | whatever type
value  | whatever type
number | int
Index: group

I then have rows like this:

group     | value         | number
-------------------------------------
Foo       | foo           | 1
Foo       | turnips       | 2
Bar       | albatross     | 3
Bar       | monkey        | 4

I want to receive results like this:

group     | value
-----------------------
Foo       | turnips
Bar       | monkey

Currently, I do this in my application by ordering by the number and only
using the last value. I imagine that this is something that can be done in
the new Postgres 9, with a sorted group by - something like this:

SELECT group, LAST(value, ORDER BY number) FROM table GROUP BY group

Is this something that is already built in, or would I have to write my
own LAST aggregate function?

Matthew

--
 The third years are wandering about all worried at the moment because they
 have to hand in their final projects. Please be sympathetic to them, say
 things like "ha-ha-ha", but in a sympathetic tone of voice
                                        -- Computer Science Lecturer

pgsql-performance by date:

Previous
From: Jeff Davis
Date:
Subject: Re: Completely un-tuned Postgresql benchmark results: SSD vs desktop HDD
Next
From: Thomas Kellerer
Date:
Subject: Re: Sorted group by