从SQL 2005中的自引用表获取层次结构数据
我正在使用 SQL 2005,现有数百万条记录的数据(因此数据结构无法更改),我正在尝试执行以下操作:
我有一些数据(以下示例来自另一个问题并进行了一些修改以显示什么我试图在自引用表中实现)。我需要按以下格式提取结果树:
Parent
Child1 of Parent
Child1 of Child 1
Child2 of Child 1
Child2 of Parent
Child1 of Child 2
我的实际数据深度为 9 层 - 我尝试使用 CTE 获取结果。然而,这会产生以下结果:
Parent
Child1 of Parent
Child2 of Parent
Child1 of Child 1
Child2 of Child 1
Child1 of Child 2
这是我正在使用的查询类型的示例:
create table workshop (
w_id smallint primary key,
p_id smallint,
s_date smalldatetime,
title varchar(100))
go
alter table workshop add constraint fk_wkshp foreign key (p_id)
references workshop(w_id)
go
insert into workshop (w_id, p_id, s_date, title) values (1, null,
'5/2/2007', 'Parent')
insert into workshop (w_id, p_id, s_date, title) values (2, 1,
'5/3/2007', 'Child 1 of the parent')
insert into workshop (w_id, p_id, s_date, title) values (3, 1,
'5/5/2007', 'Child 2 of the parent')
insert into workshop (w_id, p_id, s_date, title) values (4, 2,
'5/4/2007', 'Child of Child 1')
insert into workshop (w_id, p_id, s_date, title) values (5, 2,
'5/5/2007', 'Child 2 of the child of the parent')
insert into workshop (w_id, p_id, s_date, title) values (6, 3,
'5/7/2007', 'Child of the child 2')
insert into workshop (w_id, p_id, s_date, title) values (7, null,
'5/7/2007', '2nd Parent')
insert into workshop (w_id, p_id, s_date, title) values (8, 7,
'5/7/2007', 'Child of 2nd Parent')
insert into workshop (w_id, p_id, s_date, title) values (9, 7,
'5/7/2007', 'Child of 2nd Parent')
go
declare @id smallint
set @id = 1
;with events (w_id, p_id, s_date, title)
as
(
-- the anchor member
select
w_id, p_id, s_date, title
from
workshop
where w_id = @id
-- the recursive member
union all
select
w.w_id, w.p_id, w.s_date, w.title
from
workshop w
-- the key is to join to the CTE
join events e on e.w_id = w.p_id
)
select * from events
drop table workshop
go
我已经看到了与此相关的各种其他问题,但看不到我的问题的答案。最近的事情是 oracle“先验连接”,如果我使用 oracle dB,那就太好了!有什么想法吗?
干杯, 贾森
I am using SQL 2005, with existing data of millions of records (so data structure can not be changed) and I'm trying to do the following:
I have some data (following example is from another question and modified a bit to show what I am trying to achieve) in a self referencing table. I need to pull out a tree of results in the following format:
Parent
Child1 of Parent
Child1 of Child 1
Child2 of Child 1
Child2 of Parent
Child1 of Child 2
My actual data goes 9 levels deep - and I have tried to get the result using CTE. However this produces the following:
Parent
Child1 of Parent
Child2 of Parent
Child1 of Child 1
Child2 of Child 1
Child1 of Child 2
Here is an example of the sort of query I am using:
create table workshop (
w_id smallint primary key,
p_id smallint,
s_date smalldatetime,
title varchar(100))
go
alter table workshop add constraint fk_wkshp foreign key (p_id)
references workshop(w_id)
go
insert into workshop (w_id, p_id, s_date, title) values (1, null,
'5/2/2007', 'Parent')
insert into workshop (w_id, p_id, s_date, title) values (2, 1,
'5/3/2007', 'Child 1 of the parent')
insert into workshop (w_id, p_id, s_date, title) values (3, 1,
'5/5/2007', 'Child 2 of the parent')
insert into workshop (w_id, p_id, s_date, title) values (4, 2,
'5/4/2007', 'Child of Child 1')
insert into workshop (w_id, p_id, s_date, title) values (5, 2,
'5/5/2007', 'Child 2 of the child of the parent')
insert into workshop (w_id, p_id, s_date, title) values (6, 3,
'5/7/2007', 'Child of the child 2')
insert into workshop (w_id, p_id, s_date, title) values (7, null,
'5/7/2007', '2nd Parent')
insert into workshop (w_id, p_id, s_date, title) values (8, 7,
'5/7/2007', 'Child of 2nd Parent')
insert into workshop (w_id, p_id, s_date, title) values (9, 7,
'5/7/2007', 'Child of 2nd Parent')
go
declare @id smallint
set @id = 1
;with events (w_id, p_id, s_date, title)
as
(
-- the anchor member
select
w_id, p_id, s_date, title
from
workshop
where w_id = @id
-- the recursive member
union all
select
w.w_id, w.p_id, w.s_date, w.title
from
workshop w
-- the key is to join to the CTE
join events e on e.w_id = w.p_id
)
select * from events
drop table workshop
go
I have seen various other questions relating to this, but cant see an answer to my problem. The nearset thing is the oracle 'connect by prior' which would be great if I was using an oracle dB! Any ideas?
Cheers,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我已经找到了答案,那就是我加入的是主播成员而不是研讨会桌上。感谢 gjvdkamp 这篇类似的文章,它让我重新评估了错误的“真实”sql 语句。
I think I've found the answer which is that I was joining on the anchor member rather than on the workshop table. Thanks to gjvdkamp for this similar post which made me re-evaluate the 'real' sql statement which was in error.