CTE BOM查询Ordering
CTE BOM query Ordering
我正在处理 BOM(对于文档,所以我称之为 BOD)并且我希望最终得到树视图中的数据,因此我需要展开 BOM 并按每个级别进行排序。我不知道如何让排序正常工作
我在 SSMS 中工作
我有一个观点;
[ParentDocumentTitle], [ParentDocumentName], [ChildDocumentTitle], [ChildDocumentName], [PDFFilePath],
所有 Nvarchar
然后我有SQL查询
--alter procedure [dbo].[GetBODSummaryLevels]
IF EXISTS(SELECT [name] FROM tempdb.sys.tables WHERE [name] like '#BODSummary%')
BEGIN
DROP TABLE #BODSummary;
END;
DECLARE
@ParentDocTitle nvarchar(50),
@level int
SET @ParentDocTitle = 'Test Book';
SET @level = 10;
--as
begin
WITH RPL (
[Level],
[ParentDocumentTitle],
[ParentDocumentName],
[ChildDocumentTitle],
[ChildDocumentName],
[PDFFilePath])
AS
( SELECT
0,
ROOT.[ParentDocumentTitle],
ROOT.[ParentDocumentName],
ROOT.[ChildDocumentTitle],
ROOT.[ChildDocumentName],
ROOT.[PDFFilePath]
FROM [WorkInstruictionManagerDB].[dbo].[BOD] ROOT
WHERE ROOT.[ParentDocumentTitle] = @ParentDocTitle
UNION ALL
SELECT
Parent.Level+1,
CHILD.[ParentDocumentTitle],
CHILD.[ParentDocumentName],
CHILD.[ChildDocumentTitle],
CHILD.[ChildDocumentName],
CHILD.[PDFFilePath]
FROM RPL PARENT, [WorkInstruictionManagerDB].[dbo].[BOD] CHILD
WHERE PARENT.[ChildDocumentTitle] = CHILD.[ParentDocumentTitle]and PARENT.Level<@level
)
SELECT
[Level] as bodlevel,
[ParentDocumentTitle] as pt,
[ParentDocumentName] as pn,
[ChildDocumentTitle] as ct,
[ChildDocumentName] as cn,
[PDFFilePath] as pdf
into #BODSummary
FROM RPL
Select * From #BODSummary
end
这提供了正确的信息,但我不知道如何对这些信息进行排序,以便子级显示在父级下方。
我错过了什么或者只是做错了什么?
您可以在所有子行中使用根父 ID。然后按根 ID 和级别对行进行排序。
请试试这个:
--alter procedure [dbo].[GetBODSummaryLevels]
IF EXISTS(SELECT [name] FROM tempdb.sys.tables WHERE [name] like '#BODSummary%')
BEGIN
DROP TABLE #BODSummary;
END;
DECLARE
@ParentDocTitle nvarchar(50),
@level int
SET @ParentDocTitle = 'Test Book';
SET @level = 10;
--as
begin
WITH RPL (
[RootID],
[Level],
[ParentDocumentTitle],
[ParentDocumentName],
[ChildDocumentTitle],
[ChildDocumentName],
[PDFFilePath])
AS
( SELECT
ROOT.[ChildDocumentTitle] RootID
0 Level,
ROOT.[ParentDocumentTitle],
ROOT.[ParentDocumentName],
ROOT.[ChildDocumentTitle],
ROOT.[ChildDocumentName],
ROOT.[PDFFilePath]
FROM [WorkInstruictionManagerDB].[dbo].[BOD] ROOT
WHERE ROOT.[ParentDocumentTitle] = @ParentDocTitle
UNION ALL
SELECT
CHILD.[ParentDocumentTitle] RootID
Parent.Level+1,
CHILD.[ParentDocumentTitle],
CHILD.[ParentDocumentName],
CHILD.[ChildDocumentTitle],
CHILD.[ChildDocumentName],
CHILD.[PDFFilePath]
FROM RPL PARENT, [WorkInstruictionManagerDB].[dbo].[BOD] CHILD
WHERE PARENT.[ChildDocumentTitle] = CHILD.[ParentDocumentTitle]and PARENT.Level<@level
)
SELECT
[Level] as bodlevel,
[ParentDocumentTitle] as pt,
[ParentDocumentName] as pn,
[ChildDocumentTitle] as ct,
[ChildDocumentName] as cn,
[PDFFilePath] as pdf
into #BODSummary
FROM RPL
Select * From #BODSummary
order by RootID,Level
end
构建层次结构路径并按其排序。种类
WITH RPL (
[Level],
[ParentDocumentTitle],
[ParentDocumentName],
[ChildDocumentTitle],
[ChildDocumentName],
[PDFFilePath],
hierarchypath )
AS
( SELECT
0,
ROOT.[ParentDocumentTitle],
ROOT.[ParentDocumentName],
ROOT.[ChildDocumentTitle],
ROOT.[ChildDocumentName],
ROOT.[PDFFilePath],
convert(varchar(max), ROOT.[ChildDocumentTitle])
FROM [WorkInstruictionManagerDB].[dbo].[BOD] ROOT
WHERE ROOT.[ParentDocumentTitle] = @ParentDocTitle
UNION ALL
SELECT
Parent.Level+1,
CHILD.[ParentDocumentTitle],
CHILD.[ParentDocumentName],
CHILD.[ChildDocumentTitle],
CHILD.[ChildDocumentName],
CHILD.[PDFFilePath],
hierarchypath + '->' + CHILD.[ParentDocumentTitle]
FROM RPL PARENT
JOIN [WorkInstruictionManagerDB].[dbo].[BOD] CHILD
ON PARENT.[ChildDocumentTitle] = CHILD.[ParentDocumentTitle] and PARENT.Level<@level
)
SELECT
[Level] as bodlevel,
[ParentDocumentTitle] as pt,
[ParentDocumentName] as pn,
[ChildDocumentTitle] as ct,
[ChildDocumentName] as cn,
[PDFFilePath] as pdf,
hierarchypath
into #BODSummary
FROM RPL;
Select *
From #BODSummary
order by hierarchypath desc -- children first
我正在处理 BOM(对于文档,所以我称之为 BOD)并且我希望最终得到树视图中的数据,因此我需要展开 BOM 并按每个级别进行排序。我不知道如何让排序正常工作
我在 SSMS 中工作
我有一个观点;
[ParentDocumentTitle], [ParentDocumentName], [ChildDocumentTitle], [ChildDocumentName], [PDFFilePath],
所有 Nvarchar
然后我有SQL查询
--alter procedure [dbo].[GetBODSummaryLevels]
IF EXISTS(SELECT [name] FROM tempdb.sys.tables WHERE [name] like '#BODSummary%')
BEGIN
DROP TABLE #BODSummary;
END;
DECLARE
@ParentDocTitle nvarchar(50),
@level int
SET @ParentDocTitle = 'Test Book';
SET @level = 10;
--as
begin
WITH RPL (
[Level],
[ParentDocumentTitle],
[ParentDocumentName],
[ChildDocumentTitle],
[ChildDocumentName],
[PDFFilePath])
AS
( SELECT
0,
ROOT.[ParentDocumentTitle],
ROOT.[ParentDocumentName],
ROOT.[ChildDocumentTitle],
ROOT.[ChildDocumentName],
ROOT.[PDFFilePath]
FROM [WorkInstruictionManagerDB].[dbo].[BOD] ROOT
WHERE ROOT.[ParentDocumentTitle] = @ParentDocTitle
UNION ALL
SELECT
Parent.Level+1,
CHILD.[ParentDocumentTitle],
CHILD.[ParentDocumentName],
CHILD.[ChildDocumentTitle],
CHILD.[ChildDocumentName],
CHILD.[PDFFilePath]
FROM RPL PARENT, [WorkInstruictionManagerDB].[dbo].[BOD] CHILD
WHERE PARENT.[ChildDocumentTitle] = CHILD.[ParentDocumentTitle]and PARENT.Level<@level
)
SELECT
[Level] as bodlevel,
[ParentDocumentTitle] as pt,
[ParentDocumentName] as pn,
[ChildDocumentTitle] as ct,
[ChildDocumentName] as cn,
[PDFFilePath] as pdf
into #BODSummary
FROM RPL
Select * From #BODSummary
end
这提供了正确的信息,但我不知道如何对这些信息进行排序,以便子级显示在父级下方。
我错过了什么或者只是做错了什么?
您可以在所有子行中使用根父 ID。然后按根 ID 和级别对行进行排序。
请试试这个:
--alter procedure [dbo].[GetBODSummaryLevels]
IF EXISTS(SELECT [name] FROM tempdb.sys.tables WHERE [name] like '#BODSummary%')
BEGIN
DROP TABLE #BODSummary;
END;
DECLARE
@ParentDocTitle nvarchar(50),
@level int
SET @ParentDocTitle = 'Test Book';
SET @level = 10;
--as
begin
WITH RPL (
[RootID],
[Level],
[ParentDocumentTitle],
[ParentDocumentName],
[ChildDocumentTitle],
[ChildDocumentName],
[PDFFilePath])
AS
( SELECT
ROOT.[ChildDocumentTitle] RootID
0 Level,
ROOT.[ParentDocumentTitle],
ROOT.[ParentDocumentName],
ROOT.[ChildDocumentTitle],
ROOT.[ChildDocumentName],
ROOT.[PDFFilePath]
FROM [WorkInstruictionManagerDB].[dbo].[BOD] ROOT
WHERE ROOT.[ParentDocumentTitle] = @ParentDocTitle
UNION ALL
SELECT
CHILD.[ParentDocumentTitle] RootID
Parent.Level+1,
CHILD.[ParentDocumentTitle],
CHILD.[ParentDocumentName],
CHILD.[ChildDocumentTitle],
CHILD.[ChildDocumentName],
CHILD.[PDFFilePath]
FROM RPL PARENT, [WorkInstruictionManagerDB].[dbo].[BOD] CHILD
WHERE PARENT.[ChildDocumentTitle] = CHILD.[ParentDocumentTitle]and PARENT.Level<@level
)
SELECT
[Level] as bodlevel,
[ParentDocumentTitle] as pt,
[ParentDocumentName] as pn,
[ChildDocumentTitle] as ct,
[ChildDocumentName] as cn,
[PDFFilePath] as pdf
into #BODSummary
FROM RPL
Select * From #BODSummary
order by RootID,Level
end
构建层次结构路径并按其排序。种类
WITH RPL (
[Level],
[ParentDocumentTitle],
[ParentDocumentName],
[ChildDocumentTitle],
[ChildDocumentName],
[PDFFilePath],
hierarchypath )
AS
( SELECT
0,
ROOT.[ParentDocumentTitle],
ROOT.[ParentDocumentName],
ROOT.[ChildDocumentTitle],
ROOT.[ChildDocumentName],
ROOT.[PDFFilePath],
convert(varchar(max), ROOT.[ChildDocumentTitle])
FROM [WorkInstruictionManagerDB].[dbo].[BOD] ROOT
WHERE ROOT.[ParentDocumentTitle] = @ParentDocTitle
UNION ALL
SELECT
Parent.Level+1,
CHILD.[ParentDocumentTitle],
CHILD.[ParentDocumentName],
CHILD.[ChildDocumentTitle],
CHILD.[ChildDocumentName],
CHILD.[PDFFilePath],
hierarchypath + '->' + CHILD.[ParentDocumentTitle]
FROM RPL PARENT
JOIN [WorkInstruictionManagerDB].[dbo].[BOD] CHILD
ON PARENT.[ChildDocumentTitle] = CHILD.[ParentDocumentTitle] and PARENT.Level<@level
)
SELECT
[Level] as bodlevel,
[ParentDocumentTitle] as pt,
[ParentDocumentName] as pn,
[ChildDocumentTitle] as ct,
[ChildDocumentName] as cn,
[PDFFilePath] as pdf,
hierarchypath
into #BODSummary
FROM RPL;
Select *
From #BODSummary
order by hierarchypath desc -- children first