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 1gvmr0-0006Uf-Iy for pgsql-general@arkaria.postgresql.org; Mon, 18 Feb 2019 17:37:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gvmqy-0008Da-Sl for pgsql-general@arkaria.postgresql.org; Mon, 18 Feb 2019 17:37:16 +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 1gvmqy-0008DT-KZ for pgsql-general@lists.postgresql.org; Mon, 18 Feb 2019 17:37:16 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvmqw-0007Fh-FV for pgsql-general@postgresql.org; Mon, 18 Feb 2019 17:37:16 +0000 Received: from [192.168.127.1] (port=17131 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gvmqu-0004Tm-3q; Mon, 18 Feb 2019 17:37:12 +0000 Received: from [127.0.0.1] (port=63333 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gvmqt-000Igb-AS; Mon, 18 Feb 2019 17:37:11 +0000 From: Andrew Gierth To: Chuck Martin Cc: pgsql-general Subject: Re: HAVING query structured wrong In-Reply-To: (Chuck Martin's message of "Mon, 18 Feb 2019 12:20:02 -0500") Message-ID: <87va1hhun8.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Mon, 18 Feb 2019 17:37:10 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Chuck" == Chuck Martin writes: Chuck> I am trying to create a query that returns all transactions for Chuck> each person who has a balance over a given amount. I thought Chuck> HAVING was the answer, but if so, I'm mis-using it. This query Chuck> returns only transactions exceeding the given amount rather than Chuck> transactions for people whose balance is over the amount: [snip] Chuck> Since that returned the wrong set of records, I created another Chuck> that returns the correct set of people with balances over the Chuck> given amount. But I can't figure out how to use this to get all Chuck> the transactions for people returned by this query: Chuck> SELECT case_pkey Chuck> FROM trans,ombcase,status Chuck> WHERE case_fkey = case_pkey Chuck> AND status_fkey = status_pkey Chuck> AND statusopen = 1 Chuck> AND transistrust <> 1 Chuck> AND transcleared <> 1 Chuck> GROUP BY case_pkey Chuck> HAVING sum(transamount) >= 50 Chuck> ORDER BY case_pkey Chuck> So how do I get all transactions for each case_pkey? You can join the result of any subquery as if it were a table, either with or without using a CTE: SELECT ... FROM (select case_pkey from ... having ...) AS cases, trans WHERE trans.case_fkey = cases.case_pkey; (incidentally, please qualify all the column references in your query with a table name or alias, otherwise people reading your code have no idea which column is supposed to be in which table) or with a CTE, WITH cases AS (select ... from ... having ...) SELECT ... FROM cases, trans WHERE trans.case_fkey = cases.case_pkey; There's also a third method with window functions instead of GROUP BY, which is to do something like SELECT ... FROM (select ..., sum(transamount) over (partition by case_pkey) as total_amt from ...) s WHERE total_amt > 50; -- Andrew (irc:RhodiumToad)