gpt4 book ai didi

sql - 在postgresql中将行转换为字符串

转载 作者:行者123 更新时间:2023-11-29 11:20:23 24 4
gpt4 key购买 nike

表名是t1。字段名称是 name1。 name1 的值是这样的

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

但我想得到这样的结果

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

所以我这样尝试

    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

但它的返回

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

如何得到我想要的结果?

我正在使用 Postgresql 9.3

最佳答案

你可以试试这个:

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

string_agg

关于sql - 在postgresql中将行转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21526058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com