DB2 递归函数&常用函数


oracle 递归学习
create table a_test(
parentid varchar(2),
subid varchar(2)
)
insert into a_test(parentid,subid) values ('1','2');
insert into a_test(parentid,subid) values ('1','3');
insert into a_test(parentid,subid) values ('2','4');
insert into a_test(parentid,subid) values ('2','5');
insert into a_test(parentid,subid) values ('3','6');
insert into a_test(parentid,subid) values ('3','7');
insert into a_test(parentid,subid) values ('5','8');
insert into a_test(parentid,subid) values ('5','9');
commit;
select t.parentid ,t.subid ,level(关键字) from a_teststart withsubid='7' connect by subid =prior(关键字) partentid order by level
desc ;(递归查询叶子节点是7的所有父节点)
db2递归查询
--建表
create table family(
person_in integer,
name varchar(10),
parent_id integer
);
--插入数据
insert into family (person_id,name,parent_id) values(1,'Apple',2);
insert into family (person_id,name,parent_id) values(2,'Zoe',4);
insert into family (person_id,name,parent_id) values(3,'Tom',4);
insert into family (person_id,name,parent_id) values(4,'John',7);
insert into family (person_id,name,parent_id) values(5,'Peter',6);
insert into family (person_id,name,parent_id) values(6,'Robert',10);
insert into family (person_id,name,parent_id) values(7,'Mary',null);
insert into family (person_id,name,parent_id) values(8,'Jenny',7);
insert into family (person_id,name,parent_id) values(9,'Sam',10);
insert into family (person_id,name,parent_id) values(10,'Zhangsan',null);
--with 语句实现递归查询
with temp (persion_id,name,parent_id,LEVEL) AS (select person_id,name,parent_id,level,0 from family where person_id ='1'
--找寻person_id=1的人的祖先 union all select a.person_id ,a.name,a.parent_id,b.level+1 from family a,temp b where a.person_id
=b.parent_id)
--获取某个节点的父节点也可以获取到子节点
with temp (persion_id,name,parent_id,LEVEL) AS
(select person_id,name,parent_id,level,1
from family
where person_id ='4' --找寻person_id=1的人的祖先
union all
select
a.person_id ,
a.name,
a.parent_id,
b.level+1
from family a ,temp b
where a.person_id =b.parent_id)
--db2 中窗口函数
row_number() over (partition by a order by a ) as rn
--db2 常用时间函数
DATE(ARG)
DATE 函数返回一个日期,时间戳、日期字符串、时间戳字符串中的日期
select date('2021-08-12 16.24.18.121212') from user_test
time(函数)
DATE 函数返回一个日期,时间戳、日期字符串、时间戳字符串参数的日期
timestamp函数
返回一个或两个参数的时间戳
year函数返回一个日期,时间戳,日期字符串,时间字符串的年部分。
month函数返回月
--LTRIM,RTRIM 去掉char,varchar,graphic或者vargraphic左右的空格。
--coalesce返回参数集中第一个非null参数
coalesce(a,b,c.....)
--lcase,lower 返回定长,变长字符串的小写形式
--ucase,upper 返回大写形式
--left,right 函数返回arg最左边,最右边的length个字符串,arg可以是char或binary string。
--contact 函数返回两个字符串的连接
contact(arg1,arg2)
--repeat 函数返回arg1被重复num次数的字符串
repeate(arg,2)
--replacereplace函数用ex3代替ex1中所有出现的ex2
replace(ex1,ex2,ex3)
--substr函数返回arg1中pos位置开始的length个字符,如果没有指定length ,则返回全部的字符。
--decode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值