Picking the first of an order in an aggregate query

From: Robert James <srobertjames(at)gmail(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Picking the first of an order in an aggregate query
Date: 2012-12-31 14:33:25
Message-ID: CAGYyBgi7Jf-=SHDQwnDC6YO+dp_f+sNL6=Ax=5wAwgFhLRhG9w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a query

SELECT grouping_field, MIN(field_a), MIN(field_b)
FROM ...
GROUP BY grouping_field

But, instead of picking the MIN field_a and MIN field_b, I'd like to
pick field_a and field_b from the first record, according to an order
I'll specify. In pseudo-SQL, it would be something like this:

SELECT grouping_field, FIRST(field_a), FIRST(field_b)
FROM ...
ORDER BY field_c DESC, field_d ASC, myfunc(field_e) ASC
GROUP BY grouping_field

How can I do that with Postgres?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jack Christensen 2012-12-31 14:43:39 Re: Picking the first of an order in an aggregate query
Previous Message Philipp Kraus 2012-12-31 13:41:51 Re: rights for schema