这样的输出结果?请大力帮忙????

borwin 2003-12-18 09:05:50
有一下数据

Fid Fitemid
1 01
2 01.02
3 01.02.01
4 01.02.01.01
5 01.02.01.01.01
6 01.02.01.01.02
7 01.02.01.01.03
8 01.02.01.02
9 01.02.01.03
10 01.02.01.04
14 01.03
15 01.03.01
16 01.03.01.01
17 01.03.01.01.01

我想能得到以下的输出结果
--上一级的Fid 级别 是否已是最后
Fid Fitemid Fparentid Flevel ISLAST
1 01 0 1 0
2 01.02 1 2 3 0
3 01.02.01 2 3 0
4 01.02.01.01 3 4 0
5 01.02.01.01.01 4 5 1
6 01.02.01.01.02 4 5 1
7 01.02.01.01.03 4 5 1
8 01.02.01.02 3 4 1
9 01.02.01.03 3 4 1
10 01.02.01.04 3 4 1
14 01.03 1 2 0
15 01.03.01 14 3 0
16 01.03.01.01 15 4 0
17 01.03.01.01.01 16 5 1
...全文
23 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
erigido 2003-12-18
  • 打赏
  • 举报
回复
只有学习的份了
victorycyz 2003-12-18
  • 打赏
  • 举报
回复
select a.*
(case when a.fid=b.fid then 0 else b.fid end) as fparentid,
(len(a.fitemid)+1)/3 as flevel,
(case when c.fitemid is null then 1 else 0 end) as islast
from
( select *,
( case when len(fitemid)=2 then fitemid
else left(fitemid,len(fitemid)-3)
end
) as pitemid
from tablename
) a
left join tablename b on a.pitemid=b.fitemid
left join
( select distinct left(fitemid,len(fitemid)-3) as fitemid
from tablename
where len(fitemid)>2
) c
on a.fitemid=c.fitemid
txlicenhe 2003-12-18
  • 打赏
  • 举报
回复
测试:
create table t1(fid int,fitemid varchar(20))
insert t1 select 1,'01'
union all select 2,'01.02'
union all select 3,'01.02.01'
union all select 4,'01.02.01.01'
union all select 5,'01.02.01.01.01'
union all select 6,'01.02.01.01.02'
union all select 7,'01.02.01.01.03'
union all select 8,'01.02.01.02'
union all select 9,'01.02.01.03'
union all select 10,'01.02.01.04'
union all select 14,'01.03'
union all select 15,'01.03.01'
union all select 16,'01.03.01.01'
union all select 17,'01.03.01.01.01'

select a.fid,a.fitemid,
(case when len(fitemid)<=3 then 0 else (select fid from t1 where fitemid = left(a.fitemid,len(a.fitemid)-3) and len(a.fitemid)>3) end)
as fparentid
,(len(a.fitemid)-3)/2 +1as flevel
,(case when exists(select 1 from t1 where len(fitemid) > len(a.fitemid) and left(fitemid,len(a.fitemid)) = a.fitemid) then 0 else 1 end)
from t1 a

fid fitemid fparentid flevel
----------- -------------------- ----------- ----------- -----------
1 01 0 1 0
2 01.02 1 2 0
3 01.02.01 2 3 0
4 01.02.01.01 3 5 0
5 01.02.01.01.01 4 6 1
6 01.02.01.01.02 4 6 1
7 01.02.01.01.03 4 6 1
8 01.02.01.02 3 5 1
9 01.02.01.03 3 5 1
10 01.02.01.04 3 5 1
14 01.03 1 2 0
15 01.03.01 14 3 0
16 01.03.01.01 15 5 0
17 01.03.01.01.01 16 6 1

(所影响的行数为 14 行)



zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
--下面是测试

--测试数据
declare @t table(Fid int,Fitemid varchar(20))
insert into @t
select 1,'01'
union all select 2,'01.02'
union all select 3,'01.02.01'
union all select 4,'01.02.01.01'
union all select 5,'01.02.01.01.01'
union all select 6,'01.02.01.01.02'
union all select 7,'01.02.01.01.03'
union all select 8,'01.02.01.02'
union all select 9,'01.02.01.03'
union all select 10,'01.02.01.04'
union all select 14,'01.03'
union all select 15,'01.03.01'
union all select 16,'01.03.01.01'
union all select 17,'01.03.01.01.01'

--查询
select Fid,Fitemid
,Fparentid=isnull((select max(fid) from @t where a.Fitemid like Fitemid+'.%'),0)
,Flevel=(select sum(1) from @t where a.Fitemid like Fitemid+'%')
,ISLAST=case when exists(select 1 from @t where Fitemid like a.Fitemid+'.%') then 0 else 1 end
from @t a

/*--测试结果

(所影响的行数为 14 行)

Fid Fitemid Fparentid Flevel ISLAST
----------- -------------------- ----------- ----------- -----------
1 01 0 1 0
2 01.02 1 2 0
3 01.02.01 2 3 0
4 01.02.01.01 3 4 0
5 01.02.01.01.01 4 5 1
6 01.02.01.01.02 4 5 1
7 01.02.01.01.03 4 5 1
8 01.02.01.02 3 4 1
9 01.02.01.03 3 4 1
10 01.02.01.04 3 4 1
14 01.03 1 2 0
15 01.03.01 14 3 0
16 01.03.01.01 15 4 0
17 01.03.01.01.01 16 5 1

(所影响的行数为 14 行)
--*/
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
select Fid,Fitemid
,Fparentid=isnull((select max(fid) from 表 where a.Fitemid like Fitemid+'.%'),0)
,Flevel=(select sum(1) from 表 where a.Fitemid like Fitemid+'%')
,ISLAST=case when exists(select 1 from 表 where Fitemid like a.Fitemid+'.%') then 0 else 1 end
from 表 a
jxpanda 2003-12-18
  • 打赏
  • 举报
回复
可以通过“.”来判断吧,新手只是有这个想法,见笑了。
borwin 2003-12-18
  • 打赏
  • 举报
回复
自己up

34,576

社区成员

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

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