From: Niklas Johansson <spot@tele2.se>
To: pgsql-sql@postgresql.org
Subject: Re: Ugly group by problem
Date: Thu, 30 Mar 2006 01:49:18 +0200
Message-ID: <5047BA59-331B-4F6B-AF2C-9C9210E3CAE0@tele2.se> (raw)
In-Reply-To: <Pine.LNX.4.44.0603291838520.5673-100000@matrix.gatewaynet.com>
References: <Pine.LNX.4.44.0603291838520.5673-100000@matrix.gatewaynet.com>
On 29 mar 2006, at 17.42, Achilleus Mantzios wrote:
> The _int_union trick is to force the arrays to have unique values.
> The order by has the meaning that '{5,23}' and '{23,5}' should be
> treated
> the same way.
I didn't have the _int_union function. Is it internal? What PG-
version did you use? I solved it using DISTINCT instead (perhaps
making it a little simpler as well):
SELECT
ARRAY(SELECT DISTINCT feat_id FROM linktest WHERE
link_id=t1.link_id ORDER BY feat_id) AS feat_group,
SUM(other)
FROM linktext t1
GROUP BY 1;
feat_group | sum
-----------+-----
{2} | 1
{5,23} | 13
{23} | 14
(3 rows)
Of course I haven't tested the performance on a table containing a
lot of data... I'm not sure how the planner will treat the ARRAY-
construct. You might be able to speed it up by turning it into a
function marked STABLE, something like this:
CREATE OR REPLACE FUNCTION feat_group(INTEGER) RETURNS INTEGER[] AS $$
SELECT ARRAY(SELECT DISTINCT feat_id FROM linktest WHERE link_id=$1
ORDER BY feat_id);
$$ LANGUAGE sql STABLE;
SELECT
feat_group(link_id),
SUM(other)
FROM linktext t1
GROUP BY 1;
Sincerely,
Niklas Johansson
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-sql@postgresql.org
Cc: spot@tele2.se
Subject: Re: Ugly group by problem
In-Reply-To: <5047BA59-331B-4F6B-AF2C-9C9210E3CAE0@tele2.se>
References: <Pine.LNX.4.44.0603291838520.5673-100000@matrix.gatewaynet.com> <5047BA59-331B-4F6B-AF2C-9C9210E3CAE0@tele2.se>
* 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