How to select avg(select max(something) from ...)

From: dfgpostgres <dfgpostgres3(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: How to select avg(select max(something) from ...)
Date: 2025-02-18 20:56:27
Message-ID: CAAcmDX8Xs5+NbSV1UGUsj06yQpC=wYj_FOfTMZNvFe6uXDTqcw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi:
psql 15.3

I have a table with sets of observations, each set sharing an id.
I want to get the average of the max of each set.

id | val
-----------
1 5.0
1 4.3
1 3.8
2 4.8
2 6.0
2 2.9
3 4.1
3 4.4
3 8.0

So I want the avg of the max of the set where id=1 (5.0), where id=2 (6.0),
where id=3 (8.0) ~= 6.33...

I tried this...

select
avg(x.maxsz)
from
dvm.dvm_events d,
(select cast(max(size_g) as int) as maxsz
from dvm.wa_du_profile_data
where dvm_id=d.dvm_id) x
where
d.project='foo' and
<more conditions on d>

It doesn't like that reference to "d.dvm_id) in that subquery.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Rowley 2025-02-18 21:06:21 Re: How to select avg(select max(something) from ...)
Previous Message Nathan Bossart 2025-02-18 19:56:09 Re: Disabling vacuum truncate for autovacuum