0

I have the following table with two fields.

Table:

CREATE TABLE str_agg
(
cola varchar(50),
colb varchar(50)
);

Records:

insert into str_agg values('Alex','Student');
insert into str_agg values('Mak','Student');
insert into str_agg values('John','Teacher');
insert into str_agg values('Tony','Teacher');

I want to display the result in the comma separated format like as shown below:

Expected Result:

result
---------------------------------------------------------
Alex(Student),Mak(Student),John(Teacher),Tony(Teacher)

My try:

select string_agg(cola,'('||colb||'),') Result
from str_agg;

Getting result:

result
---------------------------------------------------------
Alex(Student),Mak(Teacher),John(Teacher),Tony
1
  • 2
    Why not generate full string first cola || '(' || colb || ')' and than pass it to string_agg with ',' delimiter ? Commented Mar 24, 2017 at 8:44

1 Answer 1

3

You are passing the value '('||colb||'),' as the delimiter.

You want:

select string_agg(cola||'('||colb||')', ',') Result
from str_agg;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.