如果仅仅是3级的,自连接应该可以了。
[php]
SQL> With b As (
2Select 1 Id,'总公司' leftcode,'北京分公司' rightcode From dual Union All
3Select 2 Id,'总公司' leftcode,'深圳分公司' rightcode From dual Union All
4Select 3 Id,'总公司' leftcode,'宁波分公司' rightcode From dual Union All
5Select 4 Id,'北京分公司' leftcode,'东城区公司' rightcode From dual Union All
6Select 5 Id,'北京分公司' leftcode,'西城区公司' rightcode From dual )
7select b1.leftcode "01",b1.rightcode "02",b2.rightcode "03" from
8(select leftcode,rightcode from b where leftcode='总公司' ) b1,
9(select leftcode,rightcode from b where leftcode!='总公司' ) b2
10where b1.rightcode=b2.leftcode(+)
11/
01 02 03
---------- ---------- ----------
总公司 北京分公司 东城区公司
总公司 北京分公司 西城区公司
总公司 宁波分公司
总公司 深圳分公司
SQL>
[/php]
|