Sql server SSRS-分组-文件夹层次结构
我有下表,数据如下:, 表名:tblFolder FolderId | FolderName | ParentFolderId 1 | A | Null 2 | B | 1 3 | C | 2 4 | D | Null 5 | E | 4 FolderId | FolderName | ParentFolderId 1 | A |空 2 | B | 1 3 | C | 2 4 | D |空 5 | E | 4 ParentFolderId将FolderId用作ex的父文件夹。文件夹a是文件夹B的父文件夹 我想以以下格式显示上述数据是SSRS报告 -A -B -C -D -E -A -B -C -D -E 所以我的问题是以上面的格式显示报表我的sql查询应该是什么,或者我应该采用什么方法?Sql server SSRS-分组-文件夹层次结构,sql-server,ssrs-2008,Sql Server,Ssrs 2008,我有下表,数据如下:, 表名:tblFolder FolderId | FolderName | ParentFolderId 1 | A | Null 2 | B | 1 3 | C | 2 4 | D | Null 5 | E | 4 FolderId | FolderName | ParentFolderId 1 | A |空 2
我不想使用递归层次结构,我想通过使用列分组来实现这一点,我们可能需要使用CTE并使用字符串进行一些替换工作,如下所示:
;With cte as
(
Select *, convert(varchar(10), Concat('-',FolderName)) as Levl from #Folder where ParentFolderId is null
Union all
Select f.Folderid, f.FolderName, f.ParentFolderId, convert(varchar(10),concat(' ',Replace(c.Levl, right(c.levl,len(f.folderName)),f.folderName))) as Levl from cte c inner join #Folder f
on c.Folderid = f.ParentFolderId
)
select Levl from cte
order by Folderid
+------+
| Levl |
+------+
| -A |
| -B |
| -C |
| -D |
| -E |
+------+
输出如下:
;With cte as
(
Select *, convert(varchar(10), Concat('-',FolderName)) as Levl from #Folder where ParentFolderId is null
Union all
Select f.Folderid, f.FolderName, f.ParentFolderId, convert(varchar(10),concat(' ',Replace(c.Levl, right(c.levl,len(f.folderName)),f.folderName))) as Levl from cte c inner join #Folder f
on c.Folderid = f.ParentFolderId
)
select Levl from cte
order by Folderid
+------+
| Levl |
+------+
| -A |
| -B |
| -C |
| -D |
| -E |
+------+
下面是一个标准的递归cte,但我们添加了一个生成的序列以确保正确的表示顺序/嵌套 --为2008年调整-- 示例
Declare @YourTable Table ([FolderId] varchar(50),[FolderName] varchar(50),[ParentFolderId] varchar(50))
Insert Into @YourTable Values
(1,'A',NULL)
,(2,'B',1)
,(3,'C',2)
,(4,'D',NULL)
,(5,'E',4)
Declare @Top int = null --<< Sets top of Hier Try 2
Declare @Nest varchar(25) = '|-----' --<< Optional: Added for readability
;with cteP as (
Select Seq = cast(10000+Row_Number() over (Order by FolderName) as varchar(500))
,FolderId
,ParentFolderId
,Lvl=1
,FolderName
From @YourTable
Where IsNull(@Top,-1) = case when @Top is null then isnull(ParentFolderId ,-1) else FolderId end
Union All
Select Seq = cast(p.Seq+'.'+cast(10000+Row_Number() over (Order by r.FolderName) as varchar(25) ) as varchar(500))
,r.FolderId
,r.ParentFolderId
,p.Lvl+1
,r.FolderName
From @YourTable r
Join cteP p on r.ParentFolderId = p.FolderId)
Select A.FolderId
,A.ParentFolderId
,A.Lvl
,FolderName = Replicate(@Nest,A.Lvl-1) + A.FolderName
From cteP A
Order By Seq
您的任务是计算标识,即前导空格的计数。恐怕最好的方法是递归计算检查SSRS层次结构报告谢谢回复。在SSRS中,我确实希望显示为FolderName格式,那么在哪一列上可以设置行摸索?