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 1tkUoe-004WVD-7C for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2025 21:07:40 +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 1tkUnd-000uUb-I6 for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2025 21:06:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tkUnd-000uUS-7W for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2025 21:06:37 +0000 Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tkUnb-001dFH-1J for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2025 21:06:37 +0000 Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-30a2f240156so26613721fa.3 for ; Tue, 18 Feb 2025 13:06:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739912794; x=1740517594; 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=hYyy7uSDwq/fHcoZgoagM5mQjcQOTYb3+mgP/bXzYqk=; b=K0eY+RsW0jnKF+8G71lm4EDiMRPaoe9MSumNsX7pc4H7xqe/MQlZJdGIG3X6aUzoMg BkPr4rnVtAutMPQkPqGT0hKJGMviBgSmtgj4KOQRvsMdlMbUk/woy3/4ZuLfWZcZOB2r +CkvKfOIYx6UBD8chIKNMaYii5WLqvELL4WI+N6+veUvlGuLxZnoa+5ttsaOFM2Qqcm1 vkZIbvhE8hDnhJe/CFh4TCKSilGy3YvAZYjR92Jl8/3i8glO4+f3pps4ezQQjqwfvTF8 M+HeaZ1aF1Forht0vz4C0t8klbq8NTWEpYjOHEUy8kVaAEBGm1Mhf3ewlycsEkv6A2+I YndQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739912794; x=1740517594; 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=hYyy7uSDwq/fHcoZgoagM5mQjcQOTYb3+mgP/bXzYqk=; b=n6tK+inqJYaqBaOhH0H5Yk5dHBf0bZVNRUPeUqO8UXJB2BH6aIzhb/eqzVtlCNd/S/ teAvbI6E0KWPumorA26wvxuK0bIgP8IGeOMXRE/4nsfPqDvRfzMfTvOqUEGMUHHRvLIz tyMlhy4Zn6mGSFGv7Loy3Jt2sqLzVUX53z4xCOy1c0JS4t1BaNHmuOVZ6fMddt/R/z6q h3rQhuojkirDZxdinba6BvdhtPf1JmoHGcNakRS1K8aKXkFcZ1wsSqCRKIWyisKINkXI dPJx4j9QVntuY84aViZczt1fkLMqMH9BrdcK/yX0m9R2K8vTdvfkqtt/v5tQKUu+2uc8 2hkQ== X-Gm-Message-State: AOJu0YwfOqdQT3vv4aI0CozoSaZ+vRrLgWFXTo8Gp4z3hnoQ2yfbzTKN kuCI7SgjqRIpStVNntyaMORco6RNYjI5YehucVlZlOVMTHTQAB9pHYTMMu4oEhgbepEPNjZq0qG NzLQ9qT2w2QhzrU/VrcjrC8vxaFcAGxkj X-Gm-Gg: ASbGncvPVGF4zcFwWybJX8NWCXLTqhryGCt23Oh1TYl3FlFNchLRCLtxvt/TxVKsukG F+Fa6Zk/0CciIEgECj6mzITcDJ/Hsli+P/Ojm4fUkeZ1noBd+DVchqkkiN+eyGQ1gT6+8ohhsM4 cAtl81oG2SkDx4OmVQOol/gu7D2/QO3RI= X-Google-Smtp-Source: AGHT+IETxz8LRKF+5AmFH9KljmVN++bt4+y2adFp6V9zob2flI4U+MiKi9fqGMeio/tMOAoDwjoDfywSdcHJ5tENlhI= X-Received: by 2002:a05:6512:e93:b0:545:22ec:8b6b with SMTP id 2adb3069b0e04-5462ef1582emr430652e87.35.1739912793706; Tue, 18 Feb 2025 13:06:33 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Wed, 19 Feb 2025 10:06:21 +1300 X-Gm-Features: AWEUYZnpohpZLn6JEdh2CWb0KSGkzv2pBes-rE1DNf5L0yDG2IVDLKuutUajPEg Message-ID: Subject: Re: How to select avg(select max(something) from ...) To: dfgpostgres Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 19 Feb 2025 at 09:56, dfgpostgres wrote: > 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. You could use LATERAL before the subquery in the FROM clause, or you could adjust the subquery by removing the "where dvm_id=d.dvm_id" replacing it with GROUP BY dvm_id and adding that column to the SELECT list and include that in the join condition between the tables. David