---
title: "Oracle中关于树递归的一个sql分析"
author: "tree3170"
date: "2012-06-21T15:36:32Z"
tags: []
categories: ["数据库"]
slug: "oracle-sql"
draft: false
lastmod: "2012-06-21T15:36:32Z"
id: 1565645
url: "https://tree3170.iteye.com/blog/1565645"
---

总的：select distinct t.id tenantid,

```
                t.name,
                ts.scnames,
                ts.scids,
                t.remark
  from PM_TENANT t
 inner join (select tenantid,
                    max(substr(scnames, 2)) scnames,--max取scname中包含的个数最多的；substr去第二个字符后面的字符
                    max(substr(scids, 2)) scids
               from (select tenantid,
                            sys_connect_by_path(scnames, ',') scnames,
                            sys_connect_by_path(scids, ',') scids
                       from (select tenantid,
                                    scnames,
                                    scids,
                                    tenantid || rn rchild,--字符串连接
                                    tenantid || (rn - 1) rfather,rn
                               from (select ts.tenantid,
                                            s.name scnames,
                                            s.id scids,
                                            row_number() over(partition by ts.tenantid order by ts.scid) rn  --行序号：以tenenti为分界，不同则从0开始
                                       from pm_tenant_sc ts
                                       left join pm_servicecomponent s on ts.scid = s.id))
                     start with rfather like '%0'--以父节点最后的字符为0的开始
                     connect by prior rchild = rfather--prior 从父节点向下遍历子节点
                     )
              group by tenantid) ts on t.id = ts.tenantid
```

   
![](http://dl.iteye.com/upload/attachment/0069/8183/acf402be-4e6b-3417-a117-36d51229fda1.jpg)

下面来分析：

1：选出行号，不同tenentid，rn从头算，相同tenantid，rn则递增

 

```
select ts.tenantid,
             s.name scnames,
             s.id scids,
             row_number() over(partition by ts.tenantid order by ts.scid) rn  --行序号：以tenenti为分界，不同则从0开始
             from pm_tenant_sc ts
             left join pm_servicecomponent s on ts.scid = s.id
```

![](http://dl.iteye.com/upload/attachment/0069/8210/a64f8f21-3e20-38e2-a28f-3faa4b3a11f4.jpg)

 

2：得到右孩子与父亲，如10200--》10201--》10202：10200

---》coach（10201）

---》Billy（10202）

 

```
select tenantid,
                                    scnames,
                                    scids,
                                    tenantid || rn rchild,--字符串连接（tenantid+rn-->rchild）
                                    tenantid || (rn - 1) rfather,rn--tenantid+rn-1-->rfather
                               from (select ts.tenantid,
                                            s.name scnames,
                                            s.id scids,
                                            row_number() over(partition by ts.tenantid order by ts.scid) rn  --行序号：以tenenti为分界，不同则从0开始
                                       from pm_tenant_sc ts
                                       left join pm_servicecomponent s on ts.scid = s.id)
```

![](http://dl.iteye.com/upload/attachment/0069/8212/58211629-a82c-32a2-bc5f-73faefcd1296.jpg)

 

3：形成树，用逗号连接各个scnames,scids

 

```
select tenantid,
                            sys_connect_by_path(scnames, ',') scnames,
                            sys_connect_by_path(scids, ',') scids
                       from (select tenantid,
                                    scnames,
                                    scids,
                                    tenantid || rn rchild,--字符串连接
                                    tenantid || (rn - 1) rfather,rn
                               from (select ts.tenantid,
                                            s.name scnames,
                                            s.id scids,
                                            row_number() over(partition by ts.tenantid order by ts.scid) rn  --行序号：以tenenti为分界，不同则从0开始
                                       from pm_tenant_sc ts
                                       left join pm_servicecomponent s on ts.scid = s.id))
                     start with rfather like '%0'--以父节点最后的字符为0的开始
                     connect by prior rchild = rfather--prior 从父节点向下遍历子节点
```

![](http://dl.iteye.com/upload/attachment/0069/8214/b3e8e5fe-be74-3c1b-83a1-2e2d68889a15.jpg)

4：去掉前面的逗号，并且取scnames,scids最多的那项（去重复）

 

```
select tenantid,
                    max(substr(scnames, 2)) scnames,--max取scname中包含的个数最多的；substr去第二个字符后面的字符
                    max(substr(scids, 2)) scids
               from (select tenantid,
                            sys_connect_by_path(scnames, ',') scnames,
                            sys_connect_by_path(scids, ',') scids
                       from (select tenantid,
                                    scnames,
                                    scids,
                                    tenantid || rn rchild,--字符串连接
                                    tenantid || (rn - 1) rfather,rn
                               from (select ts.tenantid,
                                            s.name scnames,
                                            s.id scids,
                                            row_number() over(partition by ts.tenantid order by ts.scid) rn  --行序号：以tenenti为分界，不同则从0开始
                                       from pm_tenant_sc ts
                                       left join pm_servicecomponent s on ts.scid = s.id))
                     start with rfather like '%0'--以父节点最后的字符为0的开始
                     connect by prior rchild = rfather--prior 从父节点向下遍历子节点
                     )
              group by tenantid
```

 

![](http://dl.iteye.com/upload/attachment/0069/8216/0e78864f-423e-3209-826a-b0f4eaefcb2a.jpg)