SQL 2005 中递归查询结果的排序层次结构
我有一个包含以下列的“任务”表(TaskOrder 用于在父级范围内对子级进行排序,而不是整个表):
TaskId ParentTaskId TaskName TaskOrder
我有此 CTE 查询来返回所有行:
with tasks (TaskId, ParentTaskId, [Name]) as
(
select parentTasks.TaskId,
parentTasks.ParentTaskId,
parentTasks.[Name]
from Task parentTasks
where ParentTaskId is null
union all
select childTasks.TaskId,
childTasks.ParentTaskId,
childTasks.[Name]
from Task childTasks
join tasks
on childTasks.ParentTaskId = tasks.TaskId
)
select * from tasks
此查询返回正如您所期望的,所有任务均按其级别排序。 我如何更改它以将结果按其层次结构顺序排序,如下所示?
- Task 1 -- Task 1 Subtask 1 -- Task 1 Subtask 2 - Task 2 - Task 3
谢谢。
编辑:答案应该适用于无限数量的级别。
I've got a 'Task' table with the following columns (the TaskOrder is for ordering the children within the scope of the parent, not the entire table):
TaskId ParentTaskId TaskName TaskOrder
I've got this CTE query to return all the rows:
with tasks (TaskId, ParentTaskId, [Name]) as
(
select parentTasks.TaskId,
parentTasks.ParentTaskId,
parentTasks.[Name]
from Task parentTasks
where ParentTaskId is null
union all
select childTasks.TaskId,
childTasks.ParentTaskId,
childTasks.[Name]
from Task childTasks
join tasks
on childTasks.ParentTaskId = tasks.TaskId
)
select * from tasks
This query returns all the tasks ordered by their level as you'd expect. How can I change it to order the results into their hierarchy order as below?
- Task 1 -- Task 1 Subtask 1 -- Task 1 Subtask 2 - Task 2 - Task 3
Thanks.
Edit: The answer should work with an unlimited numbr of levels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作的一种方法是添加一个层次结构列,其中包含列表中的所有先前 ID:
请注意,这假定 TaskId 是基于字符串的 ID。 如果不是,您应该在连接之前将其转换为 varchar。
One way you could do this is to add a hierarchy column that has all previous IDs in a list:
Note that this assumes that TaskId is a string-based ID. If not, you should cast it to a varchar before concatenating it.
使用Mark方法的变体解决了该问题,但我没有保留每个节点中的节点路径,因此我可以更轻松地在树中移动它们。 相反,我将“OrderBy”列从 int 更改为左侧填充零的 varchar(3),这样我就可以将它们连接到返回的所有行的主“OrderBy”中。
返回:
它不允许无限的层次结构(每个父节点最多 10 个级别/最多 1000 个子节点 - 如果我从 0 开始 OrderBy),但足以满足我的需求。
Solved the problem using a variation of Mark's method, but I'm not retaining the node path in every node, so I can more easily move them around the tree. Instead I changed my 'OrderBy' column from an int to varchar(3) left-padded with zeros so I can concatenate them into a master 'OrderBy' for all the rows returned.
This returns:
It doesn't allow for an unlimited hierarchy (max 10 levels / max 1000 children per parent node - if I'd started the OrderBy at 0) but more than enough for my needs.
你不需要所有工会的东西,我认为这应该有效:
You don't need all that union stuff, I think this should work:
由于您没有指定“ORDER BY”,您如何期望它以任何特定顺序返回它们(除了希望查询分析器能够以某种预期的方式工作?)。
如果您希望按 ParentTaskId、TaskId 顺序排列,则在第一个 UNION 元素中选择 TaskId 作为 ParentTaskId,并选择 NULL 作为 TaskId; 那么
ORDER BY ParentTaskId, TaskId?
Since you don't specify "ORDER BY", how do you expect that it returns them in any particular order (other than hoping the query analyzer will work in some expected fashion?).
If you want it in ParentTaskId, TaskId order, then select the TaskId as ParentTaskId and NULL as TaskId in the first UNION element; then
ORDER BY ParentTaskId, TaskId?