首页 > 代码库 > Oracle多行记录合并处理
Oracle多行记录合并处理
1:效果如下图所示:
表T1:
CREATE TABLE T1 ( WEEKWORKID VARCHAR2(20) , DD VARCHAR2(20) )
表T2
CREATE TABLE T2 ( WEEKWORKID VARCHAR2(20) , NR VARCHAR2(20) )
SQL语句:
select t2.weekworkid,t2.nr,T.ddfrom t2left join ( -- 4筛选结果集 select weekworkid,max(substr(dd,2))dd from( -- 3使用sys_connect_by_path生成结果集 select weekworkid,sys_connect_by_path(dd,‘,‘)dd from( -- 2创建子节点与父节点 select weekworkid,dd,weekworkid||rn rchild,weekworkid||(rn-1)rfather from( -- 1对记录添加序号 select t1.weekworkid,t1.dd,row_number() over(partition by t1.weekworkid order by t1.dd) rn from t1 where t1.weekworkid in( select weekworkid from t2 ) ) )connect by prior rchild = rfather start with rfather like ‘%0‘ ) group by weekworkid)T on t2.weekworkid=T.weekworkidorder by weekworkid
运行结果:
Oracle多行记录合并处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。