分层查询Oracle和带点数的计算(1 1.1 1.1.1 1.1.2 1.2 1.3 1.3.1 1.3.2 1.3.3 ....)

时间:2012-06-15 11:27:42

标签: sql oracle

我有分层数据。

select lpad(' ', 4*(level-1))||NAME 
from from_sql 
where where_sql 
CONNECT BY PRIOR id = parent_id 
START WITH parent_id is null 
  • 工作,好吧。

出:

la1
    la2
    la3
        la4
    la5
        la6
        la7
la8
    la9

我需要:

1    la1
1.1        la2
1.2        la3
1.2.1            la4
1.3        la5
1.3.1            la6
1.3.2            la7
2    la8
2.1        la9

如何用点进行计算?

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT SYS_CONNECT_BY_PATH(r, '.'), name
FROM
  (SELECT rank() over (partition BY parent_id
                    ORDER BY id) r,
                id,
                name,
                parent_id
  FROM from_sql 
 WHERE where_sql) t
   CONNECT BY
   PRIOR id = parent_id START WITH parent_id IS NULL

here是一个小提琴