Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 Server中对递归查询进行排序?_Sql_Sql Server - Fatal编程技术网

如何在SQL Server中对递归查询进行排序?

如何在SQL Server中对递归查询进行排序?,sql,sql-server,Sql,Sql Server,我用这种方式编写了一个查询,但我想用下面所示的方式对它进行排序。 但是,注释未排序,因此无法获得所需的结果 create table tree_table ( id int not null, parent_id int not null, name nvarchar(30) not null ); insert into tree_table (id, parent_id, name) values (1, 0, '1Title'), (2, 0, '2Tit

我用这种方式编写了一个查询,但我想用下面所示的方式对它进行排序。 但是,注释未排序,因此无法获得所需的结果

create table tree_table
(
    id int not null, 
    parent_id int not null, 
    name nvarchar(30) not null 
);

insert into tree_table (id, parent_id, name) 
values
(1, 0, '1Title'),
(2, 0, '2Title'),
(3, 0, '3Title'),
(4, 1,  '  ㄴRE 1Title 1-1'),
(5, 1,  '  ㄴRE 1Title 1-2'),
(6, 1,  '  ㄴRE 1Title 1-3'),
(7, 2,  '  ㄴRE 2Title 1-1'),
(8, 2,  '  ㄴRE 2Title 1-2'),
(9, 2,  '  ㄴRE 2Title 1-3'),
(10, 4, '    ㄴRE 1Title 1-1-1'),
(11, 4, '    ㄴRE 1Title 1-1-2'),
(12, 4, '    ㄴRE 1Title 1-1-3'),
(13, 3, '  ㄴRE 3Title 1-1'),
(14, 1, '  ㄴRE 1Title 1-4'),
(15, 6, '    ㄴRE 3Title 1-3-1'),
(16, 0, '4Title'),
(17, 16, '  ㄴRE 4Title 1-1'),
(18, 15, '      ㄴRE 3Title 1-3-1-1'),
(19, 16, '  ㄴRE 4Title 1-2'),
(20, 17, '    ㄴRE 4Title 1-1-1');
选择查询:

WITH rcte AS
(
    SELECT 
        t.id AS 'thread',
        t.id,
        t.parent_id,
        t.name
    FROM
        tree_table t
    WHERE 
        t.parent_id = 0
    UNION ALL
    SELECT 
        r.thread,
        t.id,
        t.parent_id,
        t.name
    FROM
        tree_table t
    JOIN
        rcte r ON r.id = t.parent_id
),
sorted_threads AS
(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY MAX(r.id) DESC) AS sort_number,
        r.thread
    FROM 
        rcte r
    GROUP BY 
        r.thread
)
SELECT
    st.sort_number,
    r.id,
    r.parent_id,
    r.name
FROM
    sorted_threads st
JOIN
    rcte r ON r.thread = st.thread
ORDER BY
    st.sort_number 
结果-按上述查询排序:

sort_number id    parent_id    name
----------- ----  ----------   ----------------
1           16    0            4Title
1           17    16             ㄴRE 4Title 1-1
1           19    16             ㄴRE 4Title 1-2
1           20    17               ㄴRE 4Title 1-1-1
2           1     0            1Title
2           4     1              ㄴRE 1Title 1-1
2           5     1              ㄴRE 1Title 1-2
2           6     1              ㄴRE 1Title 1-3
2           10    4                ㄴRE 1Title 1-1-1
2           11    4                ㄴRE 1Title 1-1-2
2           12    4                ㄴRE 1Title 1-1-3
2           14    1              ㄴRE 1Title 1-4
2           15    6                ㄴRE 3Title 1-3-1
2           18    15               ㄴRE 3Title 1-3-1-1
3           3     0            3Title
3           13    3              ㄴRE 3Title 1-1
4           2     0            2Title
4           7     2              ㄴRE 2Title 1-1
4           8     2              ㄴRE 2Title 1-2
4           9     2              ㄴRE 2Title 1-3
我想这样排序-当发表评论时,该帖子应该放在顶部:

sort_number id    parent_id    name
----------- ----  ----------   ----------------
1           16    0            4Title
1           17    16             ㄴRE 4Title 1-1
1           20    17               ㄴRE 4Title 1-1-1
1           19    16             ㄴRE 4Title 1-2
2           1     0            1Title
2           14    1              ㄴRE 1Title 1-4
2           15    6                ㄴRE 3Title 1-3-1
2           18    15               ㄴRE 3Title 1-3-1-1
2           6     1              ㄴRE 1Title 1-3
2           10    4                ㄴRE 1Title 1-1-1
2           11    4                ㄴRE 1Title 1-1-2
2           12    4                ㄴRE 1Title 1-1-3
2           5     1              ㄴRE 1Title 1-2
2           4     1              ㄴRE 1Title 1-1
3           3     0            3Title
3           13    3              ㄴRE 3Title 1-1
4           2     0            2Title
4           9     2              ㄴRE 2Title 1-3
4           8     2              ㄴRE 2Title 1-2
4           7     2              ㄴRE 2Title 1-1
如何更改查询以反映上述结果


感谢您的回答

Build
varchar
递归排序键,并使用技巧进行降序排序,以便将较短的值视为较大的值

WITH sorted AS
(
    SELECT 
        right(cast (1000000 + (select coalesce(max(t2.id),t.id) from tree_table t2 where t2.parent_id = t.id )as varchar(max)),6) AS [sort_key],
        t.id,
        t.parent_id,
        t.name
    FROM
        tree_table t
),
rcte AS (
    SELECT * 
    FROM  sorted t
    WHERE 
        t.parent_id = 0
    UNION ALL
    SELECT 
        r.sort_key + t.sort_key,
        t.id,
        t.parent_id,
        t.name
    FROM
        sorted t
    JOIN
        rcte r ON r.id = t.parent_id
)
SELECT * 
FROM rcte
ORDER BY 
   stuff(replicate(cast ('9' as varchar(max)), (select max(len(r2.sort_key)) from rcte r2)),1,len(sort_key), sort_key) 
DESC;

我想我只是说你希望结果按X顺序排列是没有帮助的。什么是上下文,4Title是第一个,然后是1,然后是3,然后是带子项的2。请你编辑你的帖子,并澄清分类的依据。依我看,最后输入的注释会将整个树段浮到顶部,然后是下一个注释树。请确认。请添加您当前使用的查询,并解释为什么它不能提供您想要的结果。@DRapp默认排序方法是parent\u id,parent\u id中的注释应始终按最新顺序排序。当最新的帖子发布时,它应该在最上面。如果您正在写一篇较新的文章或对一篇旧文章发表评论,您将被带到顶部,但是,评论不会放在父id下。@Nick我已经添加了我的查询。请确认。可以有多少级别的子级?将值(21,9,'ㄴRE 2标题1-3-1’;然后结果2Title必须超过3Title。但是它位于RE 2Title 1-3下。如何重新排列它?@kudy,这个任务与原来的任务不同。如果我没有弄错,新查询必须在给定节点的所有子树中找到最新消息作为排序键。请为此单独创建一个问题。
sort_key                    id  parent_id   name
000019                      16  0   4Title
000019000020                17  16    ㄴRE 4Title 1-1
000019000020000020          20  17      ㄴRE 4Title 1-1-1
000019000019                19  16    ㄴRE 4Title 1-2
000014                      1   0   1Title
000014000015                6   1     ㄴRE 1Title 1-3
000014000015000018          15  6       ㄴRE 3Title 1-3-1
000014000015000018000018    18  15        ㄴRE 3Title 1-3-1-1
000014000014                14  1     ㄴRE 1Title 1-4
000014000012                4   1     ㄴRE 1Title 1-1
000014000012000012          12  4       ㄴRE 1Title 1-1-3
000014000012000011          11  4       ㄴRE 1Title 1-1-2
000014000012000010          10  4       ㄴRE 1Title 1-1-1
000014000005                5   1     ㄴRE 1Title 1-2
000013                      3   0   3Title
000013000013                13  3     ㄴRE 3Title 1-1
000009                      2   0   2Title
000009000009                9   2     ㄴRE 2Title 1-3
000009000008                8   2     ㄴRE 2Title 1-2
000009000007                7   2     ㄴRE 2Title 1-1