请教高手

cblearn 2004-09-01 10:46:09
1.原表
子节点 父节点 相对P001层 地址
A001 P001 1 广州
A002 A001 2 青岛
A003 A002 3 广州
A004 A003 4 广州
A005 A004 5 上海
A006 A005 6 广州

要求按某一条件排列(设 地址='广州',当记录是广州时候就递增一层)

2.要求排列:
排列后层
A001 P001 1 广州
A002 A001 1 青岛
A003 A002 2 广州
A004 A003 3 广州
A005 A004 3 上海
A006 A005 4 广州

请问如何用最高效的方法找出P001的子节点并按要求排列 请高手赐教!
...全文
116 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cblearn 2004-09-01
  • 打赏
  • 举报
回复
zjcxc(邹建) 真是个热心人 谢谢高手赐教!
cblearn 2004-09-01
  • 打赏
  • 举报
回复
可以了!谢谢!看错了!
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复
--不行你就运行下面这个测试
--示例

--示例数据
create table 原表(子节点 nvarchar(10),父节点 nvarchar(10),相对P001层 int,地址 nvarchar(10))
insert 原表 select 'A001','P001',1,'广州'
union all select 'A002','A001',2,'青岛'
union all select 'A003','A002',3,'广州'
union all select 'A004','A003',4,'广州'
union all select 'A005','A004',5,'上海'
union all select 'A006','A005',6,'广州'
go

--查询的函数
create function f_qry(
@地址 nvarchar(10)
)returns @re table(子节点 nvarchar(10),父节点 nvarchar(10),重新排序后的层 int,地址 nvarchar(10))
as
begin
declare @l int
declare @t table(id nvarchar(10),sid nvarchar(4000))

select @l=相对P001层 from 原表 where 父节点='P001'
insert @t select 子节点,子节点
from 原表 where 父节点='P001'
while @@rowcount>0
begin
set @l=@l+1
insert @t select a.子节点,b.sid+','+a.子节点
from 原表 a,@t b
where a.父节点=b.id and a.相对P001层=@l
end
insert @re select a.子节点,a.父节点,0,a.地址
from 原表 a,@t b
where a.子节点=b.id
order by b.sid

set @l=0
update @re set @l=case 地址 when @地址 then @l+1 else @l end
,重新排序后的层=@l
return
end
go

--调用函数实现查询
select * from f_qry(N'广州')
go

--删除测试
drop table 原表
drop function f_qry

/*--测试结果

子节点 父节点 重新排序后的层 地址
---------- ---------- ----------- ----------
A001 P001 1 广州
A002 A001 1 青岛
A003 A002 2 广州
A004 A003 3 广州
A005 A004 3 上海
A006 A005 4 广州
--*/
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复
你运行我上面的测试,没有理由我的正常,你的不正常吧/
cblearn 2004-09-01
  • 打赏
  • 举报
回复
select * from f_qry('广州') 就这样啊!

A001 P001 0 广州
A002 A001 0 青岛
A003 A002 0 广州
A004 A003 0 广州
A005 A004 0 上海
A006 A005 0 广州
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复
我的测试不是正确的吗?
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复
你输入什么值?
cblearn 2004-09-01
  • 打赏
  • 举报
回复
不行啊!运行结果这样的?
A001 P001 0 广州
A002 A001 0 青岛
A003 A002 0 广州
A004 A003 0 广州
A005 A004 0 上海
A006 A005 0 广州
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复
--示例

--示例数据
create table 原表(子节点 varchar(10),父节点 varchar(10),相对P001层 int,地址 varchar(10))
insert 原表 select 'A001','P001',1,'广州'
union all select 'A002','A001',2,'青岛'
union all select 'A003','A002',3,'广州'
union all select 'A004','A003',4,'广州'
union all select 'A005','A004',5,'上海'
union all select 'A006','A005',6,'广州'
go

--查询的函数
create function f_qry(
@地址 varchar(10)
)returns @re table(子节点 varchar(10),父节点 varchar(10),重新排序后的层 int,地址 varchar(10))
as
begin
declare @l int
declare @t table(id varchar(10),sid varchar(8000))

select @l=相对P001层 from 原表 where 父节点='P001'
insert @t select 子节点,子节点
from 原表 where 父节点='P001'
while @@rowcount>0
begin
set @l=@l+1
insert @t select a.子节点,b.sid+','+a.子节点
from 原表 a,@t b
where a.父节点=b.id and a.相对P001层=@l
end
insert @re select a.子节点,a.父节点,0,a.地址
from 原表 a,@t b
where a.子节点=b.id
order by b.sid

set @l=0
update @re set @l=case 地址 when @地址 then @l+1 else @l end
,重新排序后的层=@l
return
end
go

--调用函数实现查询
select * from f_qry('广州')
go

--删除测试
drop table 原表
drop function f_qry

/*--测试结果

子节点 父节点 重新排序后的层 地址
---------- ---------- ----------- ----------
A001 P001 1 广州
A002 A001 1 青岛
A003 A002 2 广州
A004 A003 3 广州
A005 A004 3 上海
A006 A005 4 广州
--*/

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧