Re: Sorted group by

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Sorted group by
Date: 2010-08-10 15:56:46
Message-ID: i3rsrr$23e$1@dough.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Matthew Wakeling wrote on 10.08.2010 17:40:
> 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?

No. It's built in (8.4) and it's called Windowing functions:
http://www.postgresql.org/docs/8.4/static/tutorial-window.html
http://www.postgresql.org/docs/8.4/static/functions-window.html

SELECT group, last_value(value) over(ORDER BY number)
FROM table

You don't need the group by then (but you can apply e.g. an ORDER BY GROUP)

Thomas

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Matthew Wakeling 2010-08-10 16:03:40 Re: Sorted group by
Previous Message Matthew Wakeling 2010-08-10 15:40:16 Sorted group by