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 1tkVui-004fHS-5N for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2025 22:18:00 +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 1tkVug-001Mc9-Fk for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2025 22:17:58 +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 1tkVue-001Mb9-UO for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2025 22:17:58 +0000 Received: from fhigh-a4-smtp.messagingengine.com ([103.168.172.155]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tkVud-001b0J-0L for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2025 22:17:56 +0000 Received: from phl-compute-13.internal (phl-compute-13.phl.internal [10.202.2.53]) by mailfhigh.phl.internal (Postfix) with ESMTP id C5E5D1140101; Tue, 18 Feb 2025 17:17:53 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-13.internal (MEProxy); Tue, 18 Feb 2025 17:17:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1739917073; x=1740003473; bh=IxyhT63Gd2VbUQoV/bHYYozWn0bNQalzaSXPBklVsIQ=; b= O6WJm7aNEhf/dmArX+SfAZX6h0jI69i+Oj7qWE41Bgh58AUcpeyLroUtjsQL8p5l L8hYM94HW4h2OnVXTiDzb+UXsqFzmoNrpO5q39MhqA0sWSi1AJ4qCdSOzM2WplRZ cCY3zuubL0nEc0qWUkJSVJ/k7q4aW/4ge1edvamUc6bE2VX+uKHjNJoOYQRWRJ0+ 5Fss04CBko6SBQhPrIExZubjL7MVz/eCw3/QBvPpBHLjl21k4bYLh+q4nAF//eG/ LmkhMDOU0wywkPL4I0PxVJCTrutA6ILIXbVW9butAuYaqFTnULN/eCyRAAqY4rT2 t4fuKeSgISRMnt2ZhRK6eA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1739917073; x=1740003473; bh=I xyhT63Gd2VbUQoV/bHYYozWn0bNQalzaSXPBklVsIQ=; b=TJzqoXOguxWvg7jmY C5SxayIcT34rEzzwbOl6t+rSwl7WtLT2zonMSXaz7ox/a7PgbcIm9+MFRHz5zKqP 9RQYsgusLI0OUDrH8R/MDmW/SHqxMZfYH83Bt7Fn+EtG6x05+YxXHhWkkJLrLRsf U1P58XIMCZ53VYaUXkPwcNQpPKsG0xGKsoF/ht+aPB2plkVR+bdxLSCoP/qKD4TV dyyd8+KdDYjDsoHOj/eRXA/uwokbzDSxIKywdalon5Ex9PSHgZML1M0X5K0Ns4nQ WRj/nRg3hwJdSIFwYYJiJ7bMiXkTgbrQH/UNfVIwNp8DfGDj4Mr5JNC2sRVX1/Hz HScow== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeivdehtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgg gfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghnucfmlhgrvhgv rhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrg htthgvrhhnpeffleegieefgfevudehtdfhkeeutdffjeevgeffgeejvedthefgudeiteef heejheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthho pedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegufhhgphhoshhtghhrvghsfe esghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehlihhs thhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 18 Feb 2025 17:17:53 -0500 (EST) Message-ID: <0878c968-a3d6-4803-a5b8-3ac1eb876309@aklaver.com> Date: Tue, 18 Feb 2025 14:17:52 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: How to select avg(select max(something) from ...) To: dfgpostgres , pgsql-general@lists.postgresql.org References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/18/25 12:56, dfgpostgres wrote: > 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 >                   > > It doesn't like that reference to "d.dvm_id) in that subquery. create table wa_du_profile_data (id integer, val float); insert into wa_du_profile_data values (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); with max_val as (select max(val) from wa_du_profile_data group by id) select avg(max) from max_val; 6.333333333333333 -- Adrian Klaver adrian.klaver@aklaver.com