Sql語句裡的遞迴查詢

2021-09-22 11:14:16 字數 3371 閱讀 9581

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,0,'a')

insert into aaa values(2,0,'b')

insert into aaa values(3,1,'c')

insert into aaa values(4,1,'d')

insert into aaa values(5,2,'e')

insert into aaa values(6,3,'f')

insert into aaa values(7,3,'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](

[**] [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  ** = 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,

tbwhere  tb.** = my1.** + 1

)select *

from   my1

sqlserver2008使用表示式遞迴查詢

--由父項遞迴下級 

with cte(id,parentid,text) 

as (--父項 

select id,parentid,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,parentid,text from cte

--由子級遞迴父項 

with cte(id,parentid,text) 

as (--下級父項 

select id,parentid,text from treeview where id = 450 

union all 

--遞迴結果集中的父項 

select t.id,t.parentid,t.text from treeview as t 

inner join cte as c on t.id = c.parentid 

) select id,parentid,text from cte

分類: 

msmq,

oracle

Sql語句裡的遞迴查詢

以前使用oracle,覺得它的遞迴查詢很好用,就研究了一下sqlserver,發現它也支援在sql裡遞迴查詢 舉例說明 sqlserver2005版本的sql如下 比如乙個表,有id和pid欄位,id是主鍵,pid表示它的上級節點,表結構和資料 create table aaa id int nul...

sql語句遞迴查詢

表結構 給出乙個結點找到該節點的所有 子 節點 with c depts as select dept.from department dept where dept.pptr 父節點id union all select dept.from c depts department dept wher...

SQL語句遞迴查詢

通過sql語句遞迴查詢所有下級或上級使用者 1.ms sql with cte as select id,pid,deptname,0as lvl from department where id 2union allselect d.id,d.pid,d.deptname,lvl 1 from c...