sys_connect_by_path函数用法

sys_connect_by_path函数是用来返回组成层级的直到当前行的值。

例如:

select *
  from (select ltrim(sys_connect_by_path(id, ','), ',') id,
               ltrim(sys_connect_by_path(name, ','), ',') name,
               level as lvl,
               lpad(' ', 2 * (level - 1)) || id,
               lpad(' ', 2 * (level - 1)) || name,
               lead(level) over(order by id) leadlvlorder,
               case
                 when (level - lead(level) over(order by id)) < 0 then
                  0
                 else
                  1
               end isleaf
          from table_name c
         start with id = 0
        connect by prior c.id = c.parent_id)
 where isleaf = 1;

CONNECT_BY_ISLEAF 也可以判断叶子节点,如下:

select *
  from (select ltrim(sys_connect_by_path(id, ','), ',') id,
               ltrim(sys_connect_by_path(name, ','), ',') name,
               level as lvl,
               lpad(' ', 2 * (level - 1)) || id,
               lpad(' ', 2 * (level - 1)) || name,
               CONNECT_BY_ISLEAF isLeaf
          from table_name c
         start with id = 0
        connect by prior c.id = c.parent_id)
 where isleaf = 1;

猜你喜欢

转载自mukeliang.iteye.com/blog/1704543
今日推荐