掌握start with connect by 应用:按顺序列转行

编程技术  /  houtizong 发布于 3年前   94
通过下边的例子展示了start with  connect by 的用法。
start with expr 可以理解为,从expr处我要形成树,也就是起点;
connect by expr 表示expr成立时才放到树中作为一个节点,同样也连接了上层和下层。
create table treetest (col1 varchar2(4),col2 varchar2(4));insert into treetest values ('1','你');insert into treetest values ('1','好');insert into treetest values ('1','哦');insert into treetest values ('1','好');insert into treetest values ('2','天');insert into treetest values ('2','上');insert into treetest values ('2','地');insert into treetest values ('2','下');insert into treetest values ('3','美');insert into treetest values ('3','丽');insert into treetest values ('3','一');insert into treetest values ('3','天');//查出行号,便于排序select rownum num,t3.col1,t3.col2 from treetest t3;//forConnectBy区分不同树,connect by 处用,forStartWith在同树中的顺序,start with 处用select t1.col1 c1,to_number(t1.col1)+row_number() over (order by t1.num) forConnectBy,row_number() over(partition by t1.col1 order by t1.col1) forStartWith,t1.col2 c3 from (select rownum num,t3.col1,t3.col2 from treetest t3) t1;//sys_connect_by_path(expr,'分隔符'),start with 从这里开始找树,connect by 连接成一棵树的条件select replace(max(sys_connect_by_path(t2.c3,'/')),'/','') from (select t1.col1 c1,to_number(t1.col1)+row_number() over (order by t1.num) forConnectBy,row_number() over(partition by t1.col1 order by t1.col1) forStartWith,t1.col2 c3 from (select rownum num,t3.col1,t3.col2 from treetest t3) t1) t2 start with t2.forStartWith = 1  connect by prior t2.forConnectBy = t2.forConnectBy - 1 group by t2.c1 order by t2.c1;

输出结果:你好哦好   天上地下  美丽一天

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客