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 1gePtx-00067F-Qn for pgsql-general@arkaria.postgresql.org; Tue, 01 Jan 2019 19:40:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gePtw-0003Bm-3l for pgsql-general@arkaria.postgresql.org; Tue, 01 Jan 2019 19:40:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gePtu-0003Bf-TL for pgsql-general@lists.postgresql.org; Tue, 01 Jan 2019 19:40:31 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gePtq-0006TN-Ay for pgsql-general@postgresql.org; Tue, 01 Jan 2019 19:40:30 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2756321FAE; Tue, 1 Jan 2019 14:40:24 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 01 Jan 2019 14:40:24 -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=c 1UxC0VEvVB96OBKFfLvzycgW1Bm3d6qz97EJoUMx+8=; b=1CXYnI9MFAnc+GLOD LyNcPo72PsEifrxlxatal7pyR4u38u5j/ZVw+oN6c08M+nm5ZPifLzsVPYyycwQH d6AMI0Dh7fTNCb2pzS0uO8yG5uNu2/Ow4NMUGYWSoQMIfJspujCbop1gdmp9+lbg +eXr20w/NMaDPZ/Z24QzNEQKZ8iBAy/LxrcDh9ZNkgce7CPZ/ATiG/RJn9zZgXTC OcY/5NnV0OQpoWchQBdwR3XEj0Lv0ueMaFxNR/utZjhxivZIf1IW8JZIP3nTth2q NrbVpALyIa6fTWBd42A7QnxCBEeYioOuKWcYg7UA/Dwb8ixkxJymjWcXAtBCkdr7 K5Uww== 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=c1UxC0VEvVB96OBKFfLvzycgW1Bm3d6qz97EJoUMx +8=; b=Fde0eU+zWXjz8ekxDBo+2oct0V4LyjYLejX7qPJc/R3h2PXnHkmuyFPTi cq4Ozu43heg2X5HPOAegv+YMri5xjKH3Hq9eB4kEzG3UGnujyMOImBp6mwLtJBsH jmV2Hc3fZuTSqog0hyyYPLO1TYrEDWMbA3mLTZKk3v9hXm40exyc/qI18Ejmyr7q 6hKaEt9/0PZDtM1leQYZXySXYvYRUb1lWS0CVG+q/ChScCtbbm5Mtv0XTKi9/v5+ yFfvrbP8FtYs5n8KHW0XWlfytNrMSbbBtx5rnhqa1iEfy6z26k9KJRUFpwAn5bTi bebMPdOaMTYMScY6GruJFTNY+S3qQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrudeggdduvdelucdltddurdegtdekrddttd dmucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhht necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddv uddvrddufeegrdefnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 62DA510084; Tue, 1 Jan 2019 14:40:23 -0500 (EST) Subject: Re: Query help To: Chuck Martin , pgsql-general References: From: Adrian Klaver Message-ID: Date: Tue, 1 Jan 2019 11:40:22 -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:05 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 > > 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. Window Functions?: https://www.postgresql.org/docs/11/tutorial-window.html Or do something like(untested): select transactionid, amount, accountid, name from transaction join (select accountid, sum(amount) from transaction group by(accountid)) as account_sum on transaction.transactionid = account_sum.accountid and account_sum.sum >= 50 > > Chuck Martin > Avondale Software -- Adrian Klaver adrian.klaver@aklaver.com