Re: ARRAY_AGG and COUNT - Mailing list pgsql-sql

From Andreas Forø Tollefsen
Subject Re: ARRAY_AGG and COUNT
Date
Msg-id AANLkTik9+_fEan9kGDrEPchRuT1N1i62f_j1Cd0NPQ6a@mail.gmail.com
Whole thread Raw
In response to Re: ARRAY_AGG and COUNT  (bricklen <bricklen@gmail.com>)
Responses Re: ARRAY_AGG and COUNT  (bricklen <bricklen@gmail.com>)
List pgsql-sql
Great. Thanks. Do you have a suggestion on how to ignore the group id's with 0 as value?
I dont want these to be counted.

Thanks!

2011/2/17 bricklen <bricklen@gmail.com>
On Thu, Feb 17, 2011 at 6:20 AM, Andreas Forø Tollefsen
<andreasft@gmail.com> wrote:
> Hi all!
>
> I have tried the below query, but i cannot find a way to select only
> DISTINCT into the ARRAY_AGG function. I also do not want 0 to be included in
> the array or 0 to be counted.
>
> Code:
>
> SELECT priogrid_land.gid, priogrid_land.cell,
> array_to_string(array_agg(g1id), ';') AS g1list,
> array_to_string(array_agg(g2id), ';') AS g2list,
> array_to_string(array_agg(g3id), ';') AS g3list,
> count(distinct g1id) AS g1count, count(distinct g2id) AS g2count,
> count(distinct g3id) AS g3count
> INTO greg_list
> FROM "GREG", priogrid_land WHERE ST_Intersects("GREG".the_geom,
> priogrid_land.cell)
> GROUP BY priogrid_land.gid, priogrid_land.cell;
>
>
> As you see i.e. in g1list 482 is counted twice in row 2. 0 is also counted.
> The distinct works for count, but not for array_agg.
>

I don't have a version earlier than 8.4, but in 8.4+ you can use
DISTINCT in array_agg().
Eg.
array_to_string(array_agg(distinct g1id),';') as ...

pgsql-sql by date:

Previous
From: bricklen
Date:
Subject: Re: ARRAY_AGG and COUNT
Next
From: bricklen
Date:
Subject: Re: ARRAY_AGG and COUNT