oracle connect by及函数及生成等间隔的时间序列的方法

一、connect by 及相关函数的使用

-------------------------------------------

--路径关系 sys_connect_by_path(parentunit_id, ', ')

--connect_by_isleaf:叶子

--connect_by_root:根节点

--level:梯队,层级

-------------------------------------------

select level,

       unit_id,

       sys_connect_by_path(parentunit_id,', ')PATH,

       connect_by_isleaf leaf,

       connect_by_root unit_id "根节点"

  from j1_dw.etl_meta_unit t

 where unit_idin

       ('151.02','152.02','155.02','156.02','157.02','158.02')

 startwith parentunit_id ='100.02'

connect bynocycleprior unit_id = parentunit_id;

 

     LEVEL           UNIT_IDPATH                                                LEAF    根节点

---------- ------------------ ------------------------------------------------     ----     -----

         1            151.02 ,100.02                                              0    151.02

         2            152.02 ,100.02,151.02                                      0    151.02

         3            155.02 ,100.02,151.02,152.02                              0    151.02

         4            156.02 ,100.02,151.02,152.02,155.02                      0    151.02

         5            157.02 ,100.02,151.02,152.02,155.02,156.02              0    151.02

         6            158.02 ,100.02,151.02,152.02,155.02,156.02,157.02      1    151.02

/**************************************

应用--取叶子,获得完整路径

**************************************/

select level,

       unit_id,

       sys_connect_by_path(parentunit_id,',')PATH,

       connect_by_iscycle Cycle,

       connect_by_isleaf "leaf_node",

       connect_by_root unit_idas "root_node"

  from j1_dw.etl_meta_unit t

--取叶子

 where connect_by_isleaf =1

      --对层次查询结果进行过滤

   and unit_id <> connect_by_root unit_id

--层次查询的条件

 startwith parentunit_idisnotnull

connect bynocycleprior unit_id = parentunit_id

 orderby3,1,6

二、利用connect by 生成顺序数列,等间隔时间序列

select  level  as  an_number,

      (level +trunc(sysdate -3,'mm') -1)as date_list

  from dual

connect by level <31;

--result

 AN_NUMBER  DATE_LIST

----------  -----------

        1  2016/9/1

        2  2016/9/2

        3  2016/9/3

        4  2016/9/4

        5  2016/9/5

        6  2016/9/6

        7  2016/9/7

        8  2016/9/8

        9  2016/9/9

       10  2016/9/10

       11  2016/9/11

       12  2016/9/12

       13  2016/9/13

       14  2016/9/14

       15  2016/9/15

       16  2016/9/16

       17  2016/9/17

       18  2016/9/18

       19  2016/9/19

       20  2016/9/20

 

 

********************************************************************
** 欢迎转发,注明原文:blog.csdn.net/clark_xu   徐长亮的专栏
** 谢谢您的支持,欢迎关注微信公众号:clark_blog 
********************************************************************

 

你可能感兴趣的:([2016年以后],oracle,PLSQL开发)