SQL 在 postgresql 中将行转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21526058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:58:05  来源:igfitidea点击:

convert rows to string in postgresql

sqlpostgresqlpostgresql-9.3

提问by Sathish

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

表名是t1. 字段名称是name1. name1 具有这样的值

-------------------------------------------+
+       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

我正在使用Postgresql 9.3

回答by Krishnraj Rana

You can try this:

你可以试试这个:

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

string_agg

string_agg