(二) 行转列regexp | 您所在的位置:网站首页 › pg行转列sql › (二) 行转列regexp |
1.行转列 regexp_split_to_table(input_string, separator) input_string待转的字符串 separator是分隔符 例子: select regexp_split_to_table(‘aa,bb’, ‘,’) from dual 结果: postgreSQL注意: 在post库中使用‘+’来分割不行,所以一个解决办法是用replace函数用’,‘来代替‘+’,再用’,'来分割 select regexp_split_to_table(replace(‘aa+bb’, ‘+’, ‘,’), ‘,’) from dual 2.列转行 string_agg ( input_string, separator [ order by 某属性 ]) input_string待转的字符串 separator是转换成功后的字符串中的分隔符 例子: select string_agg(student_id,’,’) from student_info 结果: 假设student_info表中有student_id这个字段的数据为 student_id123则聚合后的答案: student_id1,2,3注: select string_agg(student_id,’,’ order by age) from student_info则结果是按照age顺序来排列的 |
CopyRight 2018-2019 实验室设备网 版权所有 |