有一个自调用表 table1ID, ManagerID, Title
1, NULL, CEO
2, 1, CTO
3, 2, Developer
4, 2, Designer
5, 1, COO
6, 5, Officer
....员工结构是:CEO
--CTO
--Developer
--Designer
--COO
--Officer
....
现在我想生成一个表table2,用另一种方法来记录员工的信息:ID, Level1Id, Level2Id, Level3Id, Level1Title, Level2Title, Level3Title,
1, 1, NULL, NULL, CEO, NULL, NULL
2, 1, 2, NULL, CEO, CTO, NULL
3, 1, 2, 3, CEO, CTO, Developer
4, 1, 2, 4, CEO, CTO, Designer
5, 1, 5, NULL, CEO, COO, NULL
6, 1, 5, 6, CEO, COO, Officer
1, NULL, CEO
2, 1, CTO
3, 2, Developer
4, 2, Designer
5, 1, COO
6, 5, Officer
....员工结构是:CEO
--CTO
--Developer
--Designer
--COO
--Officer
....
现在我想生成一个表table2,用另一种方法来记录员工的信息:ID, Level1Id, Level2Id, Level3Id, Level1Title, Level2Title, Level3Title,
1, 1, NULL, NULL, CEO, NULL, NULL
2, 1, 2, NULL, CEO, CTO, NULL
3, 1, 2, 3, CEO, CTO, Developer
4, 1, 2, 4, CEO, CTO, Designer
5, 1, 5, NULL, CEO, COO, NULL
6, 1, 5, 6, CEO, COO, Officer
解决方案 »
- 各位大侠,请问这个sql怎么写?
- sql2000差异备份问题
- 请教个问题
- 关于超时的问题
- 一个sql语句查询范围的问题
- 有办法在win 2000 Professional 上完全安装SQL Server 2000包括服务器端和客户端???
- 安装MYSQL5.0后,启动服务失败,报错误1067
- 删除一定行数记录sql 怎么写
- 我添加一条数据,id字段是自动递增的,我怎么让另一个字段与id同步咯?
- MSSql内部对这种子查询是如何操作的
- Exec sp_addlogin 'a51118101','a51118101','数据库名'
- 指定的所有者名称'a51118101'不存在,或者您没有使用该名称的权限
http://topic.csdn.net/u/20090213/15/b2e1adf6-e45f-41c8-8b1e-987f3c449bc7.html
SQLServer2005declare @t table ([ID] int,[ManagerID] int,[Title] varchar(9))
insert into @t
select 1,null,'CEO' union all
select 2,1,'CTO' union all
select 3,2,'Developer' union all
select 4,2,'Designer' union all
select 5,1,'COO' union all
select 6,5,'Officer'select
row_number() over(order by Level1id,level2Id,level3ID)ID
,*
from
(select
t1.ID Level1Id
,t2.ID Level2Id
,t3.ID Level3Id
,t1.Title Level1Title
,t2.Title Level2Title
,t3.Title Level3Title
from
@t t1 left join @t t2
on t2.managerID = t1.ID
left join @t t3
on t3.ManagerID = t2.ID
union all
select ID,null,null,Title,null,null
from @t
where ManagerID is null
union all
select
t1.ID,t2.ID,null
,t1.Title,t2.Title,null
from
@t t1 left join @t t2
on t2.managerID = t1.ID
where t1.ManagerID is null
)tID Level1Id Level2Id Level3Id Level1Title Level2Title Level3Title
----- ----------- ----------- ----------- ----------- ----------- -----------
1 1 NULL NULL CEO NULL NULL
2 1 2 NULL CEO CTO NULL
3 1 2 3 CEO CTO Developer
4 1 2 4 CEO CTO Designer
5 1 5 NULL CEO COO NULL
6 1 5 6 CEO COO Officer
7 2 3 NULL CTO Developer NULL
8 2 4 NULL CTO Designer NULL
9 3 NULL NULL Developer NULL NULL
10 4 NULL NULL Designer NULL NULL
11 5 6 NULL COO Officer NULL
12 6 NULL NULL Officer NULL NULL
/*
标题:查询各节点的父路径函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*//*
原始数据及要求结果如下:
--食品
--水果
--香蕉
--苹果
--蔬菜
--青菜
id pid name
----------- ----------- --------------------
1 0 食品
2 1 水果
3 1 蔬菜
4 2 香蕉
5 2 苹果
6 3 青菜要求得到各节点的父路径即如下结果:
id pid name 路径
--- --- ----- ---------------
1 0 食品 食品
2 1 水果 食品,水果
3 1 蔬菜 食品,蔬菜
4 2 香蕉 食品,水果,香蕉
5 2 苹果 食品,水果,苹果
6 3 青菜 食品,蔬菜,青菜
*/create table tb (id int , pid int , name nvarchar(20))
insert into tb values(1 , 0 , '食品')
insert into tb values(2 , 1 , '水果')
insert into tb values(3 , 1 , '蔬菜')
insert into tb values(4 , 2 , '香蕉')
insert into tb values(5 , 2 , '苹果')
insert into tb values(6 , 3 , '青菜')
go--查询各节点的父路径函数
create function f_pid(@id int) returns varchar(100)
as
begin
declare @re_str as varchar(100)
set @re_str = ''
select @re_str = name from tb where id = @id
while exists (select 1 from tb where id = @id and pid <> 0)
begin
select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
drop function f_pid