22

Table name is t1. Field name is name1. name1 has value like this

-------------------------------------------+
+       name1                              +
--------------------------------------------
|    "a_2013,AcMaster,Master"              |
|   "b_2014,AcMaster,Master"               |
|   "c_2013,a_AcMaster,a_Master"           |
|    "d_2014,a_AcMaster,a_Master"          |
|__________________________________________|

But i want to get reslut like this

  master          acmaster               text
   Master          AcMaster                a_2013,b_2014
   a_Master        a_Master                c_2013,d_2014

So i try like this

    select (string_to_array(schemaname,',')) [3] as master,(string_to_array(schemaname,','))
 [2] as acmaster,(string_to_array(schemaname,',')) [1] from appsetup.company2 
c2,appsetup.company1 c1,appsetup.companygroup cg where    c1.compno=c2.compno and 
cg.compgroupno=c1.compgroupno and c1.compno in (3,2) group by 
string_to_array(schemaname,',')) [3],
(string_to_array(schemaname,',')) [2],cg.compgroupno,schemaname order by 
cg.compgroupno

But its return

master          acmaster               text
   Master          AcMaster              ["a_2013","b_2014"]
   a_Master        a_Master              ["c_2013","d_2014"]

How to get my wanted result?

Am using Postgresql 9.3

1 Answer 1

41

You can try this:

select string_agg(name1, ',') as Name1s
from t1

string_agg

Sign up to request clarification or add additional context in comments.

1 Comment

In case your column type to aggregate is not text, you need to add type cast: select string_agg(name1::text, ',') as Name1s from t1

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.