加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

深入sql oracle递归查询

发布时间:2020-12-12 16:55:06 所属栏目:百科 来源:网络整理
导读:select name from sysobjects where xtype='u' select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名') Sql语句里的递归查询 SqlServer2005和Oracle 两个版本 以前使用Oracle,觉得它的递归查询很好用,就

select name from sysobjects where xtype='u'
select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名')

Sql语句里的递归查询 SqlServer2005和Oracle 两个版本
以前使用Oracle,觉得它的递归查询很好用,就研究了一下SqlServer,发现它也支持在Sql里递归查询

举例说明:
SqlServer2005版本的Sql如下:

比如一个表,有id和pId字段,id是主键,pid表示它的上级节点,表结构和数据:
CREATE TABLE [aaa](
[id] [int] NULL,
[pid] [int] NULL,
[name] [nchar](10)
)
GO
INSERT INTO aaa VALUES(1,'a')
INSERT INTO aaa VALUES(2,'b')
INSERT INTO aaa VALUES(3,1,'c')
INSERT INTO aaa VALUES(4,'d')
INSERT INTO aaa VALUES(5,2,'e')
INSERT INTO aaa VALUES(6,3,'f')
INSERT INTO aaa VALUES(7,'g')
INSERT INTO aaa VALUES(8,4,'h')
GO

--下面的Sql是查询出1结点的所有子结点

with my1 as(select * from aaa where id = 1
union all select aaa.* from my1,aaa where my1.id = aaa.pid
)
select * from my1 --结果包含1这条记录,如果不想包含,可以在最后加上:where id <> 1

--下面的Sql是查询出8结点的所有父结点

with my1 as(select * from aaa where id = 8
union all select aaa.* from my1,aaa where my1.pid = aaa.id
)
select * from my1;

--下面是递归删除1结点和所有子结点的语句:

with my1 as(select * from aaa where id = 1
union all select aaa.* from my1,aaa where my1.id = aaa.pid
)
delete from aaa where exists (select id from my1 where my1.id = aaa.id)

Oracle版本的Sql如下:

比如一个表,有id和pId字段,id是主键,pid表示它的上级节点,表结构和数据请参考SqlServer2005的,Sql如下:

--下面的Sql是查询出1结点的所有子结点

SELECT * FROM aaa
START WITH id = 1
CONNECT BY pid = PRIOR id

--下面的Sql是查询出8结点的所有父结点

SELECT * FROM aaa
START WITH id = 8
CONNECT BY PRIOR pid = id
今天帮别人做了一个有点意思的sql,也是用递归实现,具体如下:

假设有个销售表如下:

CREATE TABLE [tb](
[qj] [int] NULL,-- 月份,本测试假设从1月份开始,并且数据都是连续的月份,中间没有隔断
[je] [int] NULL,-- 本月销售实际金额
[rwe] [int] NULL,-- 本月销售任务额
[fld] [float] NULL -- 本月金额大于任务额时的返利点,返利额为je*fld
) ON [PRIMARY]

1月份销售金额大于任务额 返利额=金额*返利点
2月份销售金额大于任务额 返利额=(金额-1月份返利额)*返利点
3月份销售金额大于任务额 返利额=(金额-1,2月份返利额)*返利点
以后月份依次类推,销售额小于任务额时,返利为0

具体的Sql如下:

代码如下:WITH my1 AS (
SELECT *,
CASE
WHEN je > rwe THEN (je * fld)
ELSE 0
END fle,
CAST(0 AS FLOAT) tmp
FROM tb
WHERE qj = 1
UNION ALL
SELECT tb.*,
CASE
WHEN tb.je > tb.rwe THEN (tb.je - my1.fle -my1.tmp)
* tb.fld
ELSE 0
END fle,
my1.fle + my1.tmp tmp -- 用于累加前面月份的返利
FROM my1,
tb
WHERE tb.qj = my1.qj + 1
)
SELECT *
FROM my1

SQLserver2008使用表达式递归查询
--由父项递归下级
with cte(id,parentid,text)
as
(--父项
select id,text from treeview where parentid = 450
union all
--递归结果集中的下级
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.parentid = c.id
)
select id,text from cte
---------------------
--由子级递归父项
with cte(id,text)
as
(--下级父项
select id,text from treeview where id = 450
union all
--递归结果集中的父项
select t.id,t.text from treeview as t
inner join cte as c on t.id = c.parentid
)
select id,text from cte

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读