From: Hannes Erven <hannes@erven.at>
To: Rhys A.D. Stewart <rhys.stewart@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: array_agg and/or =ANY doesn't appear to be functioning as I expect
Date: Sun, 21 Jan 2018 05:00:29 +0100
Message-ID: <b388b283-ef70-a3e9-d62e-0d64760ff5f8@erven.at> (raw)
In-Reply-To: <CACg0vT=6WT93R3Utbhtc0KetQCXpuJqi08nE_xm1Ag0iwsv2BQ@mail.gmail.com>
References: <CACg0vT=6WT93R3Utbhtc0KetQCXpuJqi08nE_xm1Ag0iwsv2BQ@mail.gmail.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: hannes@erven.at, rhys.stewart@gmail.com
Subject: Re: array_agg and/or =ANY doesn't appear to be functioning as I expect
In-Reply-To: <b388b283-ef70-a3e9-d62e-0d64760ff5f8@erven.at>
References: <CACg0vT=6WT93R3Utbhtc0KetQCXpuJqi08nE_xm1Ag0iwsv2BQ@mail.gmail.com> <b388b283-ef70-a3e9-d62e-0d64760ff5f8@erven.at>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox