oracle connect by及函数及生成等间隔的时间序列的方法
2016-09-23 10:04
435 查看
一、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
********************************************************************
-------------------------------------------
--路径关系 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
********************************************************************
相关文章推荐
- oracle 函数: start with ... connect by 使用方法
- oracle sys_connect_by_path 函数 结果集连接
- SYS_CONNECT_BY_PATH函数用法 ORACLE
- struts的date标签和oracle中时间的转换,生成dto的方法
- ORACLE 中SYS_CONNECT_BY_PATH 函数
- Oracle List 功能 《=》 Oracle: wmsys.wm_concat、sys_connect_by_path、自定义函数实现行列转换
- Oracle多行转一行:wmsys.wm_concat、sys_connect_by_path、自定义函数实现行列转换
- oracle sys_connect_by_path 函数 结果集连接
- struts的date标签和oracle中时间的转换,生成dto的方法
- Oracle Connect by 使用方法
- Oracle 11g Data Guard Error 16143 Heartbeat failed to connect to standby 处理方法
- sqlserver实现oracle递归树函数例子(start with.....connect by prior.....)
- Oracle中sys_connect_by_path函数用法
- SYS_CONNECT_BY_PATH 函数用法 ORACLE
- 简单的SQl时间序列生成,每次时间间隔10分钟。
- Oracle 中统计时间间隔的方法
- ORACLE函数sys_connect_by_path
- Oracle: wmsys.wm_concat、sys_connect_by_path、自定义函数实现行列转换
- oracle connect by函数的简单应用
- Oracle 树形SQL语句,SYS_CONNECT_BY_PATH 函数