Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归SQL CTE和自定义排序_Sql_Linq_Tsql_Recursion_Common Table Expression - Fatal编程技术网

递归SQL CTE和自定义排序

递归SQL CTE和自定义排序,sql,linq,tsql,recursion,common-table-expression,Sql,Linq,Tsql,Recursion,Common Table Expression,图像您正在为线程化讨论板创建DB架构。有没有一种有效的方法来为给定的线程选择一个正确排序的列表?我写的代码很有效,但没有按照我希望的方式排序 假设您拥有以下数据: ID | ParentID ----------------- 1 | null 2 | 1 3 | 2 4 | 1 5 | 3 任何帮助都将不胜感激;谢谢 我是SQL新手,以前从未听说过hierarchyid数据类型。在阅读了来自的文章后,我决定将其融入到我的设计中。今晚我将对

图像您正在为线程化讨论板创建DB架构。有没有一种有效的方法来为给定的线程选择一个正确排序的列表?我写的代码很有效,但没有按照我希望的方式排序

假设您拥有以下数据:

ID | ParentID ----------------- 1 | null 2 | 1 3 | 2 4 | 1 5 | 3 任何帮助都将不胜感激;谢谢 我是SQL新手,以前从未听说过hierarchyid数据类型。在阅读了来自的文章后,我决定将其融入到我的设计中。今晚我将对此进行试验,如果我成功,我将发布更多信息

使现代化 使用dance2die的建议,从我的示例数据返回结果:

ID | ParentID | Level | DenseRank ------------------------------------- 15 NULL 1 1 20 15 2 1 21 20 3 1 17 22 3 1 22 15 2 2 31 15 2 3 32 15 2 4 33 15 2 5 34 15 2 6 35 15 2 7 36 15 2 8
嗯-我不确定你的结构是否最适合这个问题。在上面的查询中,我想不出如何按照您的要求对数据进行排序


我能想到的最好的方法是,如果你有一个将你的评论联系在一起的父表,比如一个主题表。如果你这样做了,你应该能够简单地将你的回复加入到其中,显然你需要包含正确的列,然后你可以按主题ID排序,级别,以获取所需的排序顺序或主题表上的任何其他信息表示排序的良好值。

考虑使用触发器存储整个层次结构,以便在字段中发生更改时进行更新

在您的示例中,此字段将具有: 1. 1.2 1.2.3 1.2.5 1.4

然后,您只需在该字段上排序,然后尝试以下操作:

create table #temp (test varchar (10))
insert into #temp (test)
select '1'
union select '1.2'
union select '1.2.3'
union select '1.2.5'
union select '1.4'
select * from #temp order by test asc
我相信你会喜欢的。 我最近发现了函数,它是根据

查看下面的代码以及CommentID是如何排序的

据我所知,您正在尝试按ParentCommentID对结果集进行分区

请注意denserank列

with Replies (CommentID, ParentCommentID, Level) as 
(
        select  c.CommentID, c.ParentCommentID, 1 as Level
        from    Comment c
        where   ParentCommentID is null and CommentID = 1

        union all

        select  c.CommentID, c.ParentCommentID, r.Level + 1 as Level
        from    Comment c
                inner join Replies r on c.ParentCommentID = r.CommentID
)
select  *,
        denserank = dense_rank() over (partition by ParentCommentID order by CommentID)
from    Replies
order by denserank

下面的结果

您必须仅使用层次化sql2008,或者使用一组字符串或字节连接。

sql诸神对您的建议感到愤怒。感谢您的建议,我一开始正试图让稠密级别工作,但没有运气。我在我的样本数据上查询了你的代码,它几乎成功了。有一排是坏的。我会发布上面的数据。
create table #temp (test varchar (10))
insert into #temp (test)
select '1'
union select '1.2'
union select '1.2.3'
union select '1.2.5'
union select '1.4'
select * from #temp order by test asc
with Replies (CommentID, ParentCommentID, Level) as 
(
        select  c.CommentID, c.ParentCommentID, 1 as Level
        from    Comment c
        where   ParentCommentID is null and CommentID = 1

        union all

        select  c.CommentID, c.ParentCommentID, r.Level + 1 as Level
        from    Comment c
                inner join Replies r on c.ParentCommentID = r.CommentID
)
select  *,
        denserank = dense_rank() over (partition by ParentCommentID order by CommentID)
from    Replies
order by denserank