db2关于with递归使用
来源:互联网 发布:sql 第几次出现 编辑:程序博客网 时间:2024/04/20 03:51
[php]
---------
在with在oracle中称为家族树!pub中oracle模块对此类的使用已经很成熟!
因为有人问及,这边简单的再探讨一下
-------------------
1.创建测试表
create table zxt_test
( id varchar(10),
ivalue varchar(20),
iname varchar(20)
)
not logged initially;
commit;
-----------
2.插入测试语句
insert into zxt_test values('1','aa','x'),('2','bb','x'),('3','bb','x'),('1','bb','y'),('2','bb','y'),('3','bb','y');
commit;
---------------
3.查看数据
select * from zxt_test;
1 'aa' 'x'
2 'bb' 'x'
3 'bb' 'x'
1 'bb' 'y'
2 'bb' 'y'
3 'bb' 'y'
----------------
4.with 的写法!
with
s as (
select row_number()over(partition by iname order by id) id1,
row_number()over(partition by iname order by id) id2,
ivalue,iname from zxt_test
)
,
t(iname,id1,id2,ivalue) as
(
select iname,id1,id2,cast(ivalue as varchar(100)) from s where id1 =1 and id2=1
union all
select t.iname,t.id1+1,t.id2,cast(s.ivalue||','||t.ivalue as varchar(100))
from s, t
where s.id2=t.id1+1 and t.iname = s.iname
)
select iname,ivalue from t where t.id1= (select max(id1) from s where s.iname = t.iname);
5.结果如下:
'x' 'bb,bb,aa'
'y' 'bb,bb,bb'
文章链接:http://www.itpub.net/thread-881890-1-1.html
有更详细的请查看我的blog!
http://myfriend2010.itpub.net/post/29012/367738
- db2关于with递归使用
- db2中关于递归(with)的使用
- db2中关于递归(with)的使用--合并行列
- db2 的 递归查询 使用 WITH
- DB2 使用with递归拆分行
- db2 with递归语句
- DB2 With递归语句
- DB2 with实现递归
- DB2中with递归的妙用
- DB2(WITH AS)SQL递归查询
- 使用DB2的with as 语句 实现树的递归查询
- 使用with递归回溯
- DB2 递归
- DB2 递归
- with 递归查询使用简记
- 关于树形结构的查询——使用with cet递归(2005及以上版本)
- 关于DB2递归查询的一些学习分享
- SqlServer with递归查询的使用
- Background:rgba()
- Input 分析
- substring 的变异--jdk版本差异
- python学习十:copy模块、collections模块中的defaultdict、pydiction完成在vim中tab补全
- 全面解读WM_NOTIFY & 消息反射
- db2关于with递归使用
- win7搭建svn服务
- lock语句的递归问题
- c/c++XML读写相关
- Android -- AppWidget 初级篇
- x86 汇编语言标志寄存器
- dedecms增加php函数功能及在列表页获取当前栏目的文章数
- Java 主线程等待子线程执行完再执行
- hdu 4455 Substrings(计数)