Re: Can any_value be used like first_value in an aggregate?

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Bruno Wolff III <bruno(at)wolff(dot)to>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Can any_value be used like first_value in an aggregate?
Date: 2024-06-26 07:50:59
Message-ID: 3998e51e47ce6b00c690cabfe4c60004ba42d8eb.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 2024-06-25 at 14:11 -0500, Bruno Wolff III wrote:
> The actual
> use case is a set of tripplets returned from a query, where I want on
> row for each distinct value in the first column, paired with the value
> in the second column, for which the third column is the largest. The
> second and third columns are effectively dependent on each other, so there
> won't be any ambiguity.

Try

SELECT DISTINCT ON (first_column)
first_column,
second_column,
third_column
FROM the_table
ORDER BY first_column, third_column DESC;

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dominique Devienne 2024-06-26 08:35:49 current_role of caller of a DEFINER function
Previous Message Shenavai, Manuel 2024-06-26 07:03:08 RE: Autovacuum, dead tuples and bloat