pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-performance@postgresql.org
Subject: Re: Sorted group by
Date: Tue, 10 Aug 2010 17:56:46 +0200
Message-ID: <i3rsrr$23e$1@dough.gmane.org> (raw)
In-Reply-To: <alpine.DEB.2.00.1008101627440.2654@aragorn.flymine.org>
References: <alpine.DEB.2.00.1008101627440.2654@aragorn.flymine.org>

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




view thread (11+ messages)  latest in thread

Message-ID: <i3rsrr$23e$1@dough.gmane.org>
Permalink:  ../i3rsrr$23e$1@dough.gmane.org/
Also on:    postgresql.org/message-id/i3rsrr$23e$1@dough.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: Sorted group by
  In-Reply-To: <i3rsrr$23e$1@dough.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox