oracle 分割字符串 regexp_substr,再分组求和

wulong1105 2011-09-22 11:53:39
有一列数据,有6行记录,有2列 objID(主键) str 具体为:
objID str(列名)
1 aa,bb
2 aa,cc
3 bb,cc
4 bb,dd
5 cc,ee
6 cc,ff

要分割每行记录的str字段值,再分组求和,得到
str sum (求和)
aa 2
bb 3
cc 4
dd 1
ee 1
ff 1

我知道分割一行记录用类似于下面sql

select regexp_substr('11;22;33;44;55;','[^;]+',1,rownum)
from dual connect by rownum<=5

其中 '11;22;33;44;55;' 代表一行记录,但我想要查询每行记录,分割再求和,请教各位哥哥妹妹们,怎么用1句sql写出来???
...全文
1351 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Persistence_x 2013-11-07
  • 打赏
  • 举报
回复
with tt as
(
select 1 objID, 'aa,bb' str from dual
union all
select 2, 'aa,cc' from dual
union all
select 3, 'bb,cc' from dual
union all
select 4, 'bb,dd' from dual
union all
select 5, 'cc,ee' from dual
union all 
select 6, 'cccdd,ff'from dual
)
select str ,count(*) from (

select objid ,str from (
select objid ,regexp_substr(str,'\w+',1,1) str from tt
union all
select objid ,regexp_substr(str,'\w+',1,2) str from tt where regexp_substr(str,'\w+',1,2) is not null 
union all
select objid ,regexp_substr(str,'\w+',1,3) str from tt where regexp_substr(str,'\w+',1,3) is not null)

) group by str
qazwsxedcwys 2013-11-04
  • 打赏
  • 举报
回复
create table tt ( objID number , str varchar2(20) ) select tt.*,tt.rowid from tt ; select str ,count(*) from ( select objid ,str from ( select objid ,regexp_substr(str,'\w+',1,1) str from tt union all select objid ,regexp_substr(str,'\w+',1,2) str from tt where regexp_substr(str,'\w+',1,2) is not null union all select objid ,regexp_substr(str,'\w+',1,3) str from tt where regexp_substr(str,'\w+',1,3) is not null ) ) group by str aa 2 bb 3 cc 4 dd 1 ee 1 ff 1
zzb13425138525 2013-11-01
  • 打赏
  • 举报
回复
以上是正解,我的业务需求也有涉及到这种情况,我就是这么做的
zzb13425138525 2013-11-01
  • 打赏
  • 举报
回复
with rs1 as ( select count(objID) objNum from myTable ) select newStr ,count(objID) from ( select t1.objID,regexp_substr(t1.str, '[^,]+', 1,rn) newStr from myTable t1, (select rownum rn from dual connect by rownum<=(select rs1.objNum from rs1)) where regexp_substr(t1.str, '[^,]+', 1,rn) is not null order by newStr ) group by newStr
guoxin_min 2012-02-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 momotou11 的回复:]

select sum(NUM), STR
from (select count(*) NUM, STR
from (select REGEXP_SUBSTR(STR, '[^;]+', 1, ROWNUM) as STR
from TABLE_NAME --写上你的表名
connect by RO……
[/Quote]


select regexp_substr('a,b,c,a,c', '[^,]', 1, level) as STR
from dual
connect by regexp_substr('a,b,c,a,c', '[^,]', 1, level) is not null;

可以避免ROWNUM的问题吧
momotou11 2011-09-23
  • 打赏
  • 举报
回复
select sum(NUM), STR
from (select count(*) NUM, STR
from (select REGEXP_SUBSTR(STR, '[^;]+', 1, ROWNUM) as STR
from TABLE_NAME --写上你的表名
connect by ROWNUM <= 1000) --这里设定的大一点,有的分出来的多
group by STR)
group by STR
lisong770818 2011-09-23
  • 打赏
  • 举报
回复
with a as
(

select 1 objID, 'aa,bb' str
from dual
union all
select 2, 'aa,cc'
from dual
union all
select 3, 'bb,cc'
from dual
union all
select 4, 'bb,dd'
from dual
union all
select 5, 'cc,ee' from dual)

select str, count(1)
from (select substr(str, 1, 2) str
from a
union all
select substr(str, 4, 5) from a)
group by str;
iqlife 2011-09-23
  • 打赏
  • 举报
回复
嵌套再SQL 里面就好了

例如:
select str,count(*)
(select (select regexp_substr(str,'[^;]+',1,rownum) as str
from dual connect by rownum<=5)
from tb)
group by str
cosio 2011-09-23
  • 打赏
  • 举报
回复

with temp as
(
select 1,'aa' a,'bb' b from dual
union all
select 2,'aa','cc' from dual
union all
select 3,'bb','cc' from dual
union all
select 4,'bb','dd' from dual
union all
select 5,'cc','ee' from dual
union all
select 6,'cc','ff' from dual
)
select a,count(a)
from
(
select a from temp
union all
select b from temp
)
group by a


--result:
1 2 aa
2 3 bb
3 4 cc
4 1 dd
5 1 ee
6 1 ff

3,496

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧