pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Bashtanov <bashtanov@imap.cc>
To: Mike Martin <redtux1@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Array_agg and dimensions in Array
Date: Wed, 16 Jan 2019 16:34:59 +0000
Message-ID: <e06a5c6e-a92e-f5e2-46a7-4e8652d6a245@imap.cc> (raw)
In-Reply-To: <CAOwYNKa-4FCUHe21WwfNSDg9p_-AMVwrW8RitqYonoY1+1+Lqg@mail.gmail.com>
References: <CAOwYNKa-4FCUHe21WwfNSDg9p_-AMVwrW8RitqYonoY1+1+Lqg@mail.gmail.com>

Hi Mike,
> I have come across a problem which I cant seem to solve in a nice way
> Basically I have a (small) table of tags
> What I need to is combine two concatenated fields with a literal value 
> as an array element.
You can create a custom aggregate function like this:

alexey@[local]/alexey=# create aggregate array_cat_agg(anyarray) (SFUNC 
= array_cat, STYPE = anyarray, COMBINEFUNC = array_cat, PARALLEL = SAFE);
CREATE AGGREGATE

And use it like this:

alexey@[local]/alexey=# select grp, array_cat_agg(array['--foo', bar || 
'=' || baz]) from (values ('g1', 'a', 'b'), ('g1', 'c', 'd'), ('g2', 
'e', 'f')) _ (grp, bar, baz) group by grp;
┌─────┬───────────────────────┐
│ grp │     array_cat_agg     │
├─────┼───────────────────────┤
│ g2  │ {--foo,e=f}           │
│ g1  │ {--foo,a=b,--foo,c=d} │
└─────┴───────────────────────┘
(2 rows)

Is that what you need?

Best, Alex

view thread (2+ messages)

Message-ID: <e06a5c6e-a92e-f5e2-46a7-4e8652d6a245@imap.cc>
Permalink:  ../e06a5c6e-a92e-f5e2-46a7-4e8652d6a245@imap.cc/
Also on:    postgresql.org/message-id/e06a5c6e-a92e-f5e2-46a7-4e8652d6a245@imap.cc

 · 

reply

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: bashtanov@imap.cc, redtux1@gmail.com
  Subject: Re: Array_agg and dimensions in Array
  In-Reply-To: <e06a5c6e-a92e-f5e2-46a7-4e8652d6a245@imap.cc>
  References: <CAOwYNKa-4FCUHe21WwfNSDg9p_-AMVwrW8RitqYonoY1+1+Lqg@mail.gmail.com> <e06a5c6e-a92e-f5e2-46a7-4e8652d6a245@imap.cc>

* 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