SQL tree data struct(6): NestedSet2AdjList
2008-03-02 11:52
274 查看
declare @personnel table(emp varchar(30), lft int, rgt int)
insert @personnel
select 'Albert', 1, 12
union all select 'Bert', 2, 3
union all select 'Chuck', 4, 11
union all select 'Donna', 5, 6
union all select 'Eddie', 7, 8
union all select 'Fred', 9, 10
select emp,
boss=(select top 1 emp from @personnel super where sub.lft between super.lft and super.rgt
and sub.emp <> super.emp order by super.lft desc)
from @personnel sub
go
declare @personnel table(emp varchar(30), lft int, rgt int)
insert @personnel
select 'Albert', 1, 12
union all select 'Bert', 2, 3
union all select 'Chuck', 4, 11
union all select 'Donna', 5, 6
union all select 'Eddie', 7, 8
union all select 'Fred', 9, 10
-- too slow
select emp=(select emp from @personnel where rgt - lft = (select max(rgt - lft) from @personnel)), boss = NULL
union all
select sub.emp, boss=super.emp
from @personnel as sub, @personnel as super
where sub.lft between super.lft and super.rgt
and sub.emp <> super.emp
and super.lft = (select max(super2.lft) from @personnel super2
where sub.lft between super2.lft and super2.rgt and sub.emp <> super2.emp)
insert @personnel
select 'Albert', 1, 12
union all select 'Bert', 2, 3
union all select 'Chuck', 4, 11
union all select 'Donna', 5, 6
union all select 'Eddie', 7, 8
union all select 'Fred', 9, 10
select emp,
boss=(select top 1 emp from @personnel super where sub.lft between super.lft and super.rgt
and sub.emp <> super.emp order by super.lft desc)
from @personnel sub
go
declare @personnel table(emp varchar(30), lft int, rgt int)
insert @personnel
select 'Albert', 1, 12
union all select 'Bert', 2, 3
union all select 'Chuck', 4, 11
union all select 'Donna', 5, 6
union all select 'Eddie', 7, 8
union all select 'Fred', 9, 10
-- too slow
select emp=(select emp from @personnel where rgt - lft = (select max(rgt - lft) from @personnel)), boss = NULL
union all
select sub.emp, boss=super.emp
from @personnel as sub, @personnel as super
where sub.lft between super.lft and super.rgt
and sub.emp <> super.emp
and super.lft = (select max(super2.lft) from @personnel super2
where sub.lft between super2.lft and super2.rgt and sub.emp <> super2.emp)
相关文章推荐
- SQL tree data struct(4): AdjList2MatPath (for printing tree struct)
- SQL tree data struct(5): AdjList2NestedSet
- SQL tree data struct(7): find the minimal cost route
- SQL tree data struct(1): adjacency list (id, parentid)
- SQL tree data struct(2): materialized path (1.2.3...)
- SQL tree data struct(3): nested set (1,12) include (2,3) & (4, 11) ...
- An example to show how to make Tree structure data table in SQL server
- Get XML tree format based on the tree data in SQL server table
- 数据结构:树[data struct: tree]
- SqlDataReader-------->DataTable
- 一步一步学Linq to sql(二):DataContext与实体
- ASE的sqlsrvr(dataserver) 的启动参数-T 汇集
- sqldatareader 和 sqldataadapter 、dateset的比较
- Xqk.Data数据框架开发指南:丰富的、灵活的查询方法(第三部分:SqlField)
- sparkSql DataFrame操作
- 用SqlDataReader返回多个结果集
- linq to sql 的DataContext的 ExecuteCommand()方法
- (DT系列六)devicetree中数据和 struct device有什么关系
- SqlDataAdapter;DataSet;DataTable;DataTableCollection 关联
- 《Spark 官方文档》Spark SQL, DataFrames 以及 Datasets 编程指南