Qui*_*ith 7 sql t-sql sql-server-2005
是否可以设计ORDER BY子句以确保两个字段(均为INT类型)的以下条件child,parent分别为此示例调用.
parent引用child,但可以为null.child必须在出现在parent有序结果集中之前显示每个值.我在第5点遇到困难.
样本无序数据:
child parent
------------
1 NULL
3 5
4 2
2 5
5 NULL
Run Code Online (Sandbox Code Playgroud)
显然既不ORDER BY a, b或ORDER BY b, a工作.事实上,我越是想到它,我不确定它甚至可以完成.鉴于这些限制,明显的情况如:
child parent
------------
1 2
2 1
Run Code Online (Sandbox Code Playgroud)
是不允许的,因为它违反了规则3和4(显然是5).
那么,我正在努力实现的目标,如果是这样,怎么样?平台是SQL Server 2005.
更新:样本数据的所需排序顺序:
child parent
------------
1 NULL
5 NULL
2 5
3 5
4 2
Run Code Online (Sandbox Code Playgroud)
对于在父列中定义非空值的每一行,该值已存在于子列中.
您可以使用递归CTE来查找每个节点的"深度",并按顺序排序:
create table node (id int, parent int)
insert into node values (1, null)
insert into node values (2, 5)
insert into node values (3, 5)
insert into node values (4, 2)
insert into node values (5, null)
insert into node values (6, 4);
with node_cte (id, depth) as (
select id, 0 from node where parent is null
union all
select node.id, node_cte.depth + 1
from node join node_cte on node.parent = node_cte.id
)
select node.*
from node
join node_cte on node_cte.id=node.id
order by node_cte.depth asc
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |