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

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
...全文
26 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
内容概要:本文详细介绍了Anaconda的下载、安装与配置方法(2025最新版)。Anaconda是一个开源的Python/R数据科学集成开发平台,预装了1500多个科学计算库,并提供conda包管理和环境管理功能。文章首先列出了系统要求,接着分别讲述了适用于不同操作系统的下载方式,包括官方下载和国内镜像下载。然后,具体讲解了Windows、macOS和Linux三种操作系统的安装步骤,以及环境变量的手动配置方法。此外,还提供了验证安装是否成功的命令和配置国内镜像源的方法,以提高下载速度。最后,列出了一些常用conda命令和常见问题的解决方案。 适合人群:从事数据科学、机器学习领域的研究人员和开发者,特别是需要频繁使用Python科学计算库的用户。 使用场景及目标:①帮助用户快速搭建Python开发环境,尤其是需要多个Python版本共存或隔离环境的情况下;②解决因网络原因导致的下载速度慢的问题;③提供详细的安装指南,确保安装过程顺利进行;④指导用户正确配置环境变量,避免常见的安装后无法使用的错误。 阅读建议:由于Anaconda涉及多平台安装和配置,建议读者根据自己的操作系统选择相应的章节重点阅读,并严格按照步骤操作。对于初次使用者,建议先从简单的安装入手,再逐步学习环境管理和包管理的相关命令。

34,838

社区成员

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

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