Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tkXfr-004r3o-8X for pgsql-general@arkaria.postgresql.org; Wed, 19 Feb 2025 00:10:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tkXfp-0028et-Rc for pgsql-general@arkaria.postgresql.org; Wed, 19 Feb 2025 00:10:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tkXfp-0028ej-Gh for pgsql-general@lists.postgresql.org; Wed, 19 Feb 2025 00:10:45 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tkXfn-001c0Z-38 for pgsql-general@lists.postgresql.org; Wed, 19 Feb 2025 00:10:44 +0000 Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-3d0465a8d34so51186395ab.0 for ; Tue, 18 Feb 2025 16:10:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739923843; x=1740528643; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=5vlAqDfBJkC0cvZ6adVgV2z/KV8Gfl9zulVkxuUH4ao=; b=MZIXrhk5Taz8Z/ammvjwpnGumMgBrHHnj2dRlbkAaGkiWo0zbi6kbUNx20lkcf2AMb YZxndS1HHzz0PcbiGHxwg2mS/dLVeWcKdeoUlL07zpUFGIt4vis3PwcpcyRJPBT/ScCv 4+Q+OesCAVdeYEcVQ6F3cD/AyCRIQDN9d/w7zbpRuKXH77iXCQbEgaDPtglxKmHZhdvt QmAX5+xQVmvOCpD4XWQXI5O0Kj9VQsZrD/ID4ai65E4S6I925GOVYMsM4gPPdlPq8+JQ GTw2ibO1jt8r2SUKzkbIU0uZNdIJw1Al/K7F77zTkvmXEVw9ZcT0QIcXaIzzs/FeHpik A4Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739923843; x=1740528643; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5vlAqDfBJkC0cvZ6adVgV2z/KV8Gfl9zulVkxuUH4ao=; b=sVNNNcGlVYdDCOTuVpEiRmYYfTIyWe6a8UJIupIv5rxGyU0oL1rpg44z/nPojDPhNP JE9uSxH+tyvbdtF+SjvcGsPefLTs/brd7gkCQFyUwHoem9euDuvYFCPHdAvtdZFyGOgC +V+FNMn1/DsqQpHDz/FBf2tRuVvBCTIuRCv8Gi1WaufNSOmOdM2DKnC8BEX/6Wpv5P+6 D26TtzZY9B5RDwXEX4vLB1G7aqe67FSbSo0wyPRq/K4bFCFHt+erJTB14NWbBydY+IOK dNXDlXJGlKzwqBzCwMHqpDSg+b4Nl+CVydL8bpE1zvnfC1kc3U+AWKfVMA+gpDAgi5Bb wopQ== X-Forwarded-Encrypted: i=1; AJvYcCVKMa2r3uoX6VV9zb1WBONXfWZdmxEQnASQSAjGbvAdPt/RxjzSoo5+8bwDvowK51wrnanZbwzqDlBlwlz4@lists.postgresql.org X-Gm-Message-State: AOJu0Ywx4my/InRVGIOuRS1f9JizrjpH7Z8mz3fYqR3hCxut6B/UN3VR 2iVj5KXJaOlfOL07s3QMnZJvqCL2t2GP6UYyW5XKxHse2ItvrN/3y6n2IXC2/JYTC9I+qsed6uQ bLIfEb4iHKVstbP7Tqcfso513Fv0= X-Gm-Gg: ASbGncsC5sVa9PY0Mcpfy9Nn7mKpifQt+sU6ft/bzCDxhrT8Fq+46kd/bJINYwo3RUe lNsLlg+g0m/ysRwZazJCtb/hwZ2gIyQg/H1CEx8GxdBhQ8EoUWrZEjfChnVqgmcQ7JjSd/Yiud0 yVhtm9DNG0F1rOwAqC6bTM37GoBklD040= X-Google-Smtp-Source: AGHT+IGkc1WUPwfKiePr2ocTtjc4Q7OSEYJW2r4jPTf+mgrriMgfnWbPi2B/ciJ3Ka3jgaPzxI23CyKM7/KVGd6B5Mk= X-Received: by 2002:a05:6e02:1b09:b0:3cf:fb97:c313 with SMTP id e9e14a558f8ab-3d2809cf122mr184919425ab.18.1739923843276; Tue, 18 Feb 2025 16:10:43 -0800 (PST) MIME-Version: 1.0 References: <0878c968-a3d6-4803-a5b8-3ac1eb876309@aklaver.com> In-Reply-To: <0878c968-a3d6-4803-a5b8-3ac1eb876309@aklaver.com> From: Greg Sabino Mullane Date: Tue, 18 Feb 2025 19:10:06 -0500 X-Gm-Features: AWEUYZkNrCbjkt5ynNWSwbsxVoeuT2q6Yj9qMc_HjEShlqRbOuDU1Hl2W4DrlXM Message-ID: Subject: Re: How to select avg(select max(something) from ...) To: Adrian Klaver Cc: dfgpostgres , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000009d6ac9062e73964d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000009d6ac9062e73964d Content-Type: text/plain; charset="UTF-8" Another variation: select avg(max) from (select distinct max(val) over(partition by id) from mytable); Cheers, Greg -- Crunchy Data - https://www.crunchydata.com Enterprise Postgres Software Products & Tech Support --0000000000009d6ac9062e73964d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Another variation:

sel= ect avg(max) from (select distinct max(val) over(partition by id) from myta= ble);


Cheers,
Greg
--
Enterprise= Postgres Software Products & Tech Support

--0000000000009d6ac9062e73964d--