首页 > Linux操作系统 > Linux操作系统 > GP行转列、列转行函数
如产品的关键词拆分。
1、 select regexp_split_to_table(keywords,E',+') from odl.e01_product where id in(10934893,50268693); 可以直接把关键词按逗号拆分成多行
SELECT t FROM regexp_split_to_table('abcdefgh', E'\\s*') AS t;
2、 select regexp_split_to_array(keywords,E',+') from odl.e01_product where id in(10934893,50268693); 可以直接把结果转成数组。指定直接取数组中某一个值
如:select a[2] from (select regexp_split_to_array(keywords,E',+') a from odl.e01_product where id in(10934893,50268693))t1;
这个结果出来是一个数据组,如果要行数据也可以。结果同方法一,只是使用函数不同
select a[b.seq] from (select regexp_split_to_array(keywords,E',+') a from odl.e01_product where id in(10934893,50268693))a,(select 1 seq
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7)b where a[b.seq] is not null;
这样要注意数据量。第一次计算时,数据量会是A表的行数乘以B行的行数。数据量大时要注意评估一下。
INSERT 0 1
Time: 40.560 ms
aligputf8=# insert into asd values(1,'yong');
INSERT 0 1
Time: 396.225 ms
aligputf8=# insert into asd values(2,'fdp');
INSERT 0 1
Time: 45.639 ms
aligputf8=# select id,string_agg(name,',') from asd group by id;
id | string_agg
----+------------
2 | fdp
1 | he,yong
(2 rows)
Time: 1352.745 ms
aligputf8=#
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9999499/viewspace-706207/ ,如需转载,请注明出处,否则将追究法律责任。