用于排序的SQL查询逻辑

时间:2016-12-30 13:40:44

标签: sql .net sql-server tsql logic

我遇到了在SQL查询中按逻辑创建订单的问题:

查询如下所示:

WITH CTE AS ( SELECT CommentID ,
                CommentUserName,
                 ReplyCommentID ,
                CommentID AS ThreadID ,
                CAST( CommentID AS VARCHAR( MAX ) ) AS PathStr,
                HtmlComment ,
                CommentPostDocumentID ,
                 CommentIsApproved,
                CommentDate 

         FROM Blog_CommentDetails AS T WITH(NOLOCK)
         WHERE ReplyCommentID IS NULL
         UNION ALL
         SELECT T.CommentID ,
                T.CommentUserName,
                T.ReplyCommentID ,
                CTE.ThreadID ,
                PathStr + '-'+ CAST( T.ReplyCommentID AS VARCHAR( MAX ) ) AS PathStr,
                T.HtmlComment ,
                t.CommentPostDocumentID ,
                 t.CommentIsApproved,
                T.CommentDate 

         FROM Blog_CommentDetails AS T WITH(NOLOCK)
         JOIN CTE 
         ON T.ReplyCommentID = CTE.CommentID
         WHERE T.ReplyCommentID IS NOT NULL)

SELECT *
    FROM CTE
    WHERE CommentPostDocumentID = 15 AND CommentIsApproved=1
    ORDER BY ThreadID, PathStr ,
            CommentDate DESC;

它显示以下结果:

Query Result

但预期的结果是: enter image description here

更改是,我需要在纪念之后插入记录 - 144因为回复纪念和纪念是相同的。

表示只要有匹配的commentid和replycommentid,在这种情况下为144,那么带有replycommentid的行应该归结为具有commentid的行。

查询答案列表:

query result for DVT

2 个答案:

答案 0 :(得分:1)

如何使用当前记录

的父记录组合的commentID字符串进行排序
WITH CTE AS ( SELECT CommentID ,
                CommentUserName,
                 ReplyCommentID ,
                CommentID AS ThreadID ,
                CAST( CommentID AS VARCHAR( MAX ) ) AS PathStr,
                HtmlComment ,
                CommentPostDocumentID ,
                 CommentIsApproved,
                CommentDate ,
                CAST('' AS VARCHAR(MAX)) AS NewPathStr -- added
         FROM Blog_CommentDetails AS T WITH(NOLOCK)
         WHERE ReplyCommentID IS NULL
         UNION ALL
         SELECT T.CommentID ,
                T.CommentUserName,
                T.ReplyCommentID ,
                CTE.ThreadID ,
                PathStr + '-'+ CAST( T.ReplyCommentID AS VARCHAR( MAX ) ) AS PathStr,
                T.HtmlComment ,
                t.CommentPostDocumentID ,
                 t.CommentIsApproved,
                T.CommentDate,              
                CTE.NewPathStr+'--'+CAST(cte.CommentID AS VARCHAR(MAX)) + '-' + CAST(t.CommentID AS VARCHAR(MAX)) AS NewPathStr  --added
         FROM Blog_CommentDetails AS T WITH(NOLOCK)
         JOIN CTE 
         ON T.ReplyCommentID = CTE.CommentID
         WHERE T.ReplyCommentID IS NOT NULL)

SELECT *  -- You might have to remove NewpathStr if you do not want to see it
    FROM CTE
    WHERE CommentPostDocumentID = 15 AND CommentIsApproved=1
    ORDER BY ThreadID, NewPathStr, PathStr ,
            CommentDate DESC;

答案 1 :(得分:0)

这是深度的第一种方法,可以使用ORDER BY hierarchyid值来完成:

(1) 订购     的ThreadId,     CONVERT(HIERARCHYID,'\'+ REPLACE(PathStr,' - ','\')+'\'),     CommentDate

(2)在递归CTE中,我将生成PathStr作为hirarchyid值。

注意:我没有测试过这个解决方案。 注意#2:抱歉格式错误:我正在手机上写这个答案。