按层次排序 SQL 查询,它是随机代码
Ordering SQL query by hierarchy and it's random code
我尝试搜索相关和相似的内容,但找不到。
这是我需要得到的 table 结果:
+-----+-----------+------+-------------------+
| ID | PARENT_ID | CODE | NAME |
+-----+-----------+------+-------------------+
| 218 | NULL | 1445 | First One |
| 235 | 218 | 2 | First Child |
| 247 | 235 | 45 | First Grandchild |
| 246 | 235 | 55 | Second Grandchild |
| 230 | 218 | 3 | Second Child |
| 238 | 230 | 12 | Third Grandchild |
| 231 | 230 | 20 | Fourth Grandchild |
+-----+-----------+------+-------------------+
顺序必须是层次结构后跟代码。
我需要这个来做出断言。而且,如果可能的话,我想让它只做一个查询,而没有对这个列表进行排序的方法。
这是我要断言的示例:
Tree Hierarchy
到目前为止我所做的是以下递归查询:
WITH CTE (ID, PARENT_ID, CODE, NAME)
AS
-- Anchor:
(SELECT
ID,
PARENT_ID,
CODE,
NAME
FROM WAREHOUSE
WHERE PARENT_ID IS NULL
UNION ALL
-- Level:
SELECT
W.ID,
W.PARENT_ID,
W.CODE,
W.NAME
FROM WAREHOUSE AS W
INNER JOIN CTE
ON R.PARENT_ID = CTE.ID)
SELECT *
FROM CTE
感谢任何帮助!
提前致谢!
看起来您可以在 hierarchyid 路径中使用 [CODE] 序列
例子
Declare @YourTable Table ([ID] int,[PARENT_ID] int,[CODE] varchar(50),[NAME] varchar(50))
Insert Into @YourTable Values
(218,NULL,1445,'First One')
,(235,218,2,'First Child')
,(247,235,45,'First Grandchild')
,(246,235,55,'Second Grandchild')
,(230,218,3,'Second Child')
,(238,230,12,'Third Grandchild')
,(231,230,20,'Fourth Grandchild')
;with cteP as (
Select ID
,PARENT_ID
,[Code]
,Name
,HierID = convert(hierarchyid,concat('/',[Code],'/'))
From @YourTable
Where Parent_ID is null
Union All
Select ID = r.ID
,PARENT_ID = r.PARENT_ID
,r.[Code]
,Name = r.Name
,HierID = convert(hierarchyid,concat(p.HierID.ToString(),r.[Code],'/'))
From @YourTable r
Join cteP p on r.PARENT_ID = p.ID)
Select Lvl = HierID.GetLevel()
,ID
,PARENT_ID
,[Code]
,Name
From cteP A
Order By A.HierID
Returns
Lvl ID PARENT_ID Code Name
1 218 NULL 1445 First One
2 235 218 2 First Child
3 247 235 45 First Grandchild
3 246 235 55 Second Grandchild
2 230 218 3 Second Child
3 238 230 12 Third Grandchild
3 231 230 20 Fourth Grandchild
我尝试搜索相关和相似的内容,但找不到。
这是我需要得到的 table 结果:
+-----+-----------+------+-------------------+
| ID | PARENT_ID | CODE | NAME |
+-----+-----------+------+-------------------+
| 218 | NULL | 1445 | First One |
| 235 | 218 | 2 | First Child |
| 247 | 235 | 45 | First Grandchild |
| 246 | 235 | 55 | Second Grandchild |
| 230 | 218 | 3 | Second Child |
| 238 | 230 | 12 | Third Grandchild |
| 231 | 230 | 20 | Fourth Grandchild |
+-----+-----------+------+-------------------+
顺序必须是层次结构后跟代码。 我需要这个来做出断言。而且,如果可能的话,我想让它只做一个查询,而没有对这个列表进行排序的方法。 这是我要断言的示例: Tree Hierarchy
到目前为止我所做的是以下递归查询:
WITH CTE (ID, PARENT_ID, CODE, NAME)
AS
-- Anchor:
(SELECT
ID,
PARENT_ID,
CODE,
NAME
FROM WAREHOUSE
WHERE PARENT_ID IS NULL
UNION ALL
-- Level:
SELECT
W.ID,
W.PARENT_ID,
W.CODE,
W.NAME
FROM WAREHOUSE AS W
INNER JOIN CTE
ON R.PARENT_ID = CTE.ID)
SELECT *
FROM CTE
感谢任何帮助! 提前致谢!
看起来您可以在 hierarchyid 路径中使用 [CODE] 序列
例子
Declare @YourTable Table ([ID] int,[PARENT_ID] int,[CODE] varchar(50),[NAME] varchar(50))
Insert Into @YourTable Values
(218,NULL,1445,'First One')
,(235,218,2,'First Child')
,(247,235,45,'First Grandchild')
,(246,235,55,'Second Grandchild')
,(230,218,3,'Second Child')
,(238,230,12,'Third Grandchild')
,(231,230,20,'Fourth Grandchild')
;with cteP as (
Select ID
,PARENT_ID
,[Code]
,Name
,HierID = convert(hierarchyid,concat('/',[Code],'/'))
From @YourTable
Where Parent_ID is null
Union All
Select ID = r.ID
,PARENT_ID = r.PARENT_ID
,r.[Code]
,Name = r.Name
,HierID = convert(hierarchyid,concat(p.HierID.ToString(),r.[Code],'/'))
From @YourTable r
Join cteP p on r.PARENT_ID = p.ID)
Select Lvl = HierID.GetLevel()
,ID
,PARENT_ID
,[Code]
,Name
From cteP A
Order By A.HierID
Returns
Lvl ID PARENT_ID Code Name
1 218 NULL 1445 First One
2 235 218 2 First Child
3 247 235 45 First Grandchild
3 246 235 55 Second Grandchild
2 230 218 3 Second Child
3 238 230 12 Third Grandchild
3 231 230 20 Fourth Grandchild