Re: select from grouped data

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Roger Mason <rmason(at)mun(dot)ca>
Cc: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: select from grouped data
Date: 2021-08-25 13:19:39
Message-ID: CAKFQuwavG_JG63EHyWxY_bM3s08Av1vu=XTzBTywLafUO0-htQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

>
>
> The output looks like:
>
> jid | timestamp | scf | energy
> ------------+-----------------+-----+----------------
> 1250_1 | 20210805-114634 | 18 | -1316.43700819
> 1250_1 | 20210805-114634 | 17 | -1316.43700825
> 1250_1 | 20210805-114634 | 16 | -1316.4370097
> 1250_1 | 20210805-114634 | 15 | -1316.43700991
> 1250_1 | 20210805-114634 | 14 | -1316.43699775
> 1250_1 | 20210805-114634 | 13 | -1316.43699117
> 1250_1 | 20210805-114634 | 12 | -1316.43750771
> 1250_1 | 20210805-114634 | 11 | -1316.43805358
> 1250_1 | 20210805-114634 | 10 | -1316.43857192
> 1250_1 | 20210805-114634 | 9 | -1316.43070942
> 1251_1 | 20210806-062539 | 18 | -1316.43700819
> 1251_1 | 20210806-062539 | 17 | -1316.43700826
> ....
>
> What I want is to get (for each group) the energy corresponding to the
> maximum value of scf.
>
>
SELECT DISTINCT ON (jid) jid, timestamp, scf, energy [...] ORDER BY jid,
scf DESC

David J.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Roger Mason 2021-08-25 13:59:10 Re: select from grouped data
Previous Message Roger Mason 2021-08-25 11:07:15 select from grouped data