1

I have a table called servers with columns server and site as laid out below:

table servers:
server       site
servern1    .biz.net
servern2    .biz.net
servern3    .biz.net

I'm trying to get an output as a string in one line as shown below

servern1.biz.net:servern1;servern2.biz.net:servern2;servern3.biz.net:servern3

and not had much luck as my attempt below shows

postgres=# select array_to_string(array_agg(server),':')||site||':'||server as vals from servers group by server,site;

          vals           
-------------------------
 servern2.biz.com:servern2
 servern3.biz.com:servern3
 servern1.biz.com:servern1
(3 rows)

Any help/advice is appreciated! Thanks

2 Answers 2

1

The following should do it:

select string_agg(server||':'||site, ';') as vals
from servers;

If either column could be empty or null, concat_ws() might be a better choice:

select string_agg(concat_ws(':', server, site), ';') as vals
from servers;
Sign up to request clarification or add additional context in comments.

Comments

1

That's ace, thank you @a_horse_with_no_name

I've just tweaked a little for the desired result as shown below

select string_agg(concat_ws('', server, site,':',server), ';') as vals from servers;

                                  vals                                   
-------------------------------------------------------------------------
servern1.biz.net:servern1;servern2.biz.net:servern2;servern3.biz.net:servern3
(1 row)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.