+ -
当前位置:首页 → 问答吧 → split column value

split column value

时间:2011-02-22

来源:互联网

今天一位同事找我了解关于拆解字段内容的问题:
有个字段,内容是
1|2|4
2|3|5
拆开然后group by
比如上面两行的结果是
字段  count
1   1
2   2
3   1
4   1
5   1
使用PostgreSQL的函数进行拆解,过程如下:
test=> create table tbl_Test (col1 text);
CREATE TABLE
test=> insert into tbl_test values(‘1,2′);
INSERT 0 1
test=> insert into tbl_test values(‘1,2′);
INSERT 0 1
test=> select t,count(*) from (select regexp_split_to_table(‘1,2,2′,’,') t from tbl_test)t group by t;
t | count
—+——-
2 |     4
1 |     2
(2 rows)
test=> select t,count(*) from (select regexp_split_to_table(col1,’,') t from tbl_test)t group by t;
t | count
—+——-
2 |     2
1 |     2
(2 rows)
test=> insert into tbl_test values(‘1,2,3,4,5,6′);
INSERT 0 1
test=> select t,count(*) from (select regexp_split_to_table(col1,’,') t from tbl_test)t group by t;
t | count
—+——-
2 |     3
4 |     1
5 |     1
6 |     1
3 |     1
1 |     3
(6 rows)
test=> select * from tbl_Test;
col1
————-
1,2
1,2
1,2,3,4,5,6
(3 rows)
select regexp_split_to_table(replace(‘1|2′,’|',’,'),’,') t;

作者: digoal   发布时间: 2011-02-22

ORACLE10G开始提供正则表达式了。

作者: renxiao2003   发布时间: 2011-02-28

相关阅读 更多

热门下载

更多