0

I have two tables, one for people and another for telephone, and some people may have more than one telephone. In a query with LEFT JOIN, it is returning duplicated lines according to the number of telephones. I would like to generate a query that returns something like the table below. Is it possible?

id  | names | phones
-----------------------------------
id1 | Name1 | phone1,phone2,phone3
id2 | Name2 | phone1
id3 | Name3 | phone1
id4 | Name4 | phone1,phone2

1 Answers1

0

Use function string_agg

select p.pid,p.name,string_agg(t.tval,',') from people p
left join telephone t
on p.tid = t.tid
group by p.pid, p.name
order by p.pid;

Refer fiddle here.

Pankaj
  • 2,692
  • 2
  • 6
  • 18