【发布时间】:2013-08-03 19:22:00
【问题描述】:
我有一个 CTE 查询,用于查找主叶和子叶。但我无法控制两个兄弟姐妹之间的叶子选择顺序:
表中的每一行都声明为:
(childID INT ,parentID INT ,NAME NVARCHAR(30),location int)
其中location 是优先排序IFF 他们是兄弟姐妹。
所以我有这个树结构:这些对有一个位置优先级:
例如:
`a` ( location=1) should be before `f` (location=2)
`b` ( location=1) should be before `e` (location=2)
`d` ( location=1) should be **before** `c` (location=2)
问题是我似乎必须首先 order by childID 才能看到正确的结构(sibling unsorted)。
但是 - 我的 order by 应该是什么样子才能看到正确的结构(&& 同级排序)?
(在我的例子中:d 应该在 c 之前)
Here is the working query which yields all the tree leafs ( unsorted siblings)
附言childID 不表示有关排序的任何内容。它只是一个占位符。正如我所说,两兄弟之间的位置是location 列。(这里,childId 是排序的,因为那是我插入行的顺序...
【问题讨论】:
-
@downvoter ,在没有评论或解释这样详细的问题的情况下投反对票 - 不是本网站的精神。 - 报告。
标签: sql-server recursion sql-order-by common-table-expression