Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gePxU-0006Es-HW for pgsql-general@arkaria.postgresql.org; Tue, 01 Jan 2019 19:44:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gePxS-0007nP-Uy for pgsql-general@arkaria.postgresql.org; Tue, 01 Jan 2019 19:44:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gePxR-0007mi-HA for pgsql-general@lists.postgresql.org; Tue, 01 Jan 2019 19:44:10 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gePxO-0005A9-7H for pgsql-general@postgresql.org; Tue, 01 Jan 2019 19:44:08 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 7B46821ECD; Tue, 1 Jan 2019 14:44:05 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 01 Jan 2019 14:44:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=L wwHD04+PtoPMsKz4gTI7YLBzPT2q+9rVOMZr7+DpR4=; b=BvAj9xd1byjkqosZ3 hryyjBV+nlA0u7ZylkYdCVx5KbmA8jlF+wkpTN7qQMsTHWO3zNUCI/SGGfMijMl8 dR17IKJL2D3D/aI4UrUkoU6QaF42WC4Xzut9vhEqJH2r5AaaU6TZOsA0W7seq3tB XXobjK5uk16cbQjJEwptC6XvRy77xo1npI2/+hEm/PfQGOv1ypkGvlo7wNIk9qPZ E8gikH0oF4Ps+tEwwRiMENGv0NMmw/wrnw0sPbztSqvNeBg15VpzpckG5tN3dzzT +Z8MaAf2LtT7n94Y6B316+8WXxwIfzBzbUkTRzI6D0h9b6+itbEuPiVQlR7L3jJK K0GBQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=LwwHD04+PtoPMsKz4gTI7YLBzPT2q+9rVOMZr7+Dp R4=; b=bMQ+3CletERoy4uyqVVOgmsFkQvMi5GpNHLIzFe9ZchkRgR4eOsKPulYc tVWjeQWFXuhCytzIDcGNaQj9b/AarilpLJtyhyUvrNh12alCABPoKnNDWwUn/Owr 9nmw/1NpXIBt6GiMPo5WUlwHjIlOEG/wG05KsHsIoae5atEOhuW9VtICjGcp7Sgd yBTrJFDLlab6g8is3mGytT6JIMhOahIlxx70Wv5AVw6CCylaD8HejL3Rsww1460+ tBZt3UJpX1cqKO/9noLPxvw6pmfxoeeJdb4fCj1yvh2ccgH9Wnfw+DGyhF38HExW v2CsNd4gAs9LgdtEcifrY5Gys0Uaw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrudeggdduvdelucdltddurdegtdekrddttd dmucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhht necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurddvuddvrddufeegrd efnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhl rghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 9C1A010085; Tue, 1 Jan 2019 14:44:04 -0500 (EST) Subject: Re: Query help To: John W Higgins , pgsql-general References: From: Adrian Klaver Message-ID: <073395bd-912c-2e4e-7892-58aa86283c52@aklaver.com> Date: Tue, 1 Jan 2019 11:44:03 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/1/19 11:26 AM, John W Higgins wrote: > On Tue, Jan 1, 2019 at 11:06 AM Chuck Martin > wrote: > > Sorry if this is too basic a question for this list, but I don't > fully get how to use aggregates (sum()) and group-by together. I'm > trying to get a list of transactions where the total for a given > account exceeds a given number. I'm not sure an example is needed, > but if so, consider this simplified data: > > accountid.   name > 1                  bill > 2.                 james > 3                  sarah > 4                  carl > > transaction > id.         amount.      accountid.     name > 1.          50.              1                   bill > 2.          25.              2                   james > 3           35               4                   carl > 4.          75.              1                   bill > 5           25.              1                   bill > 6           50               3                   sarah > > results wanted-all transactions where account total >= 50 > > id.         amount.      accountid.    name > 1.          50.              1                   bill > 3.          75.              1                   bill > 4           25.              1                   bill > 5           50               3                   sarah > > > You have 2 concepts here - identify the accounts with a total over 50 > and then show the transactions for those accounts. I prefer CTEs here > because they allow for better understanding (to me) of the steps > involved. A subquery would work here as well. > > with accounts_over_total as ( > select accountid from transactions where sum(amount) >= 50 group by > accountid) Unfortunately there is a hitch in the above:( select p_item_no from projection where sum(qty) > 100 group by(p_item_no); ERROR: aggregate functions are not allowed in WHERE LINE 1: select p_item_no, sum(qty) from projection where sum(qty) > > select transactions.* from transactions join accounts_over_total on > transactions.accountid = accounts.accountid > > John > > I've tried to understand how to use GROUP BY and HAVING, but the > penny won't drop. I keep getting errors saying that all columns in > the SELECT have to also be in the GROUP BY, but nothing I've done > seems to produce the correct results. I think because the GROUP BY > contains multiple columns, so each row is treated as a group. It > also is difficult to parse out since in the real world, many more > tables and columns are involved. > > Chuck Martin > Avondale Software > -- Adrian Klaver adrian.klaver@aklaver.com