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;
有个字段,内容是
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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28