Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert rows to string in postgresql

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

like image 536
Sathish Avatar asked Feb 03 '14 11:02

Sathish


1 Answers

You can try this:

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

string_agg

like image 132
Krishnraj Rana Avatar answered Sep 20 '22 03:09

Krishnraj Rana