Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ed6oD-0004KY-9b for pgsql-general@arkaria.postgresql.org; Sun, 21 Jan 2018 04:00:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ed6oC-0001sE-IB for pgsql-general@arkaria.postgresql.org; Sun, 21 Jan 2018 04:00:40 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ed6oC-0001rT-2l for pgsql-general@lists.postgresql.org; Sun, 21 Jan 2018 04:00:40 +0000 Received: from 80-109-180-71.cable.dynamic.surfer.at ([80.109.180.71] helo=margarita.erven.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ed6o8-0007yC-Qg for pgsql-general@postgresql.org; Sun, 21 Jan 2018 04:00:38 +0000 Received: from [192.168.27.172] (unknown [192.168.0.1]) by margarita.erven.at (Postfix) with ESMTPSA id AF2AD77004DC; Sun, 21 Jan 2018 05:00:31 +0100 (CET) Subject: Re: array_agg and/or =ANY doesn't appear to be functioning as I expect To: "Rhys A.D. Stewart" , pgsql-general@postgresql.org References: From: Hannes Erven Message-ID: Date: Sun, 21 Jan 2018 05:00:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-AT Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Rhys, Am 2018-01-21 um 02:42 schrieb Rhys A.D. Stewart: > Greetings All, > I'm having an issue which is very perplexing. The having clause in a > query doesn't appear to be working as I expect it. Either that or my > understanding of array_agg() is flawed. > > [...] > > with listing as ( > select start_vid, end_vid, array_agg(node order by path_seq) > node_array, array_agg(edge order by path_seq) edge_array > from confounded.dataset > group by start_vid,end_vid > having true =ALL (array_agg(truth)) > ) > select count(*) from confounded.dataset > where node in (select distinct unnest(node_array) from listing) and > truth = false; > > I would expect the above query to return 0 rows. the answer is in your data: "node" is not a UNIQUE field, and there are node values with multiple rows. e.g. node=977 has one row with truth=true and one with truth=false. So what your second query really does is "select all node values from listing for which another entry with truth=false exists in the dataset". Presuming that "seq" is a primary key [although not declared], you probably meant to restrict your query on that. Best regards, -hannes