按名称对嵌套集进行排序,同时保持深度完整性

发布于 2024-09-06 09:28:50 字数 1137 浏览 7 评论 0原文

我正在使用嵌套集模型,稍后将使用该模型为我的网站构建站点地图。这是我的表结构。

create table departments (
    id int identity(0, 1) primary key
    , lft int
    , rgt int
    , name nvarchar(60)
);

insert into departments (lft, rgt, name) values (1, 10, 'departments');
insert into departments (lft, rgt, name) values (2, 3, 'd');
insert into departments (lft, rgt, name) values (4, 9, 'a');
insert into departments (lft, rgt, name) values (5, 6, 'b');
insert into departments (lft, rgt, name) values (7, 8, 'c');

如何按深度和名称排序?我可以做

select
    replicate('----', count(parent.name) - 1) + ' ' + node.name
    , count(parent.name) - 1 as depth
, node.lft
from
    departments node
    , departments parent
where
    node.lft between parent.lft and parent.rgt
group by
    node.name, node.lft
order by
    depth asc, node.name asc;

但是,由于某种原因,这与孩子和父母不匹配。

department      lft     rgt
---------------------------
 departments    0       1
---- a        1        4
---- d        1        2
-------- b    2        5
-------- c    2        7

正如您所看到的,部门“d”有部门“a”的子级!

谢谢。

I'm using the nested set model that'll later be used to build a sitemap for my web site. This is my table structure.

create table departments (
    id int identity(0, 1) primary key
    , lft int
    , rgt int
    , name nvarchar(60)
);

insert into departments (lft, rgt, name) values (1, 10, 'departments');
insert into departments (lft, rgt, name) values (2, 3, 'd');
insert into departments (lft, rgt, name) values (4, 9, 'a');
insert into departments (lft, rgt, name) values (5, 6, 'b');
insert into departments (lft, rgt, name) values (7, 8, 'c');

How can I sort by depth as well as name? I can do

select
    replicate('----', count(parent.name) - 1) + ' ' + node.name
    , count(parent.name) - 1 as depth
, node.lft
from
    departments node
    , departments parent
where
    node.lft between parent.lft and parent.rgt
group by
    node.name, node.lft
order by
    depth asc, node.name asc;

However, that does not match children with their parent for some reason.

department      lft     rgt
---------------------------
 departments    0       1
---- a        1        4
---- d        1        2
-------- b    2        5
-------- c    2        7

As you can see, department 'd' has department 'a's children!

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兰花执着 2024-09-13 09:28:50

我想我终于想出了一个 ANSI SQL 解决方案。基本要点是,它计算父级与节点自己的父级之一具有较低值名称的行数,或者本身与节点处于同一级别并具有较低值名称的行数。如果需要,您需要对其进行细微调整才能添加缩进。另外,我不知道所有子查询在大型数据集上的性能如何:

SELECT
    N1.name
FROM
    dbo.departments N1
ORDER BY
    (
    SELECT
        COUNT(DISTINCT N2.lft)
    FROM
        dbo.departments N2
    INNER JOIN (
                SELECT
                    N.name,
                    N.lft,
                    N.rgt,
                    (SELECT COUNT(*) FROM dbo.departments WHERE lft < N.lft AND rgt > N.lft) AS depth
                FROM
                    dbo.departments N) SQ1 ON
        SQ1.lft <= N2.lft AND SQ1.rgt >= N2.lft
    INNER JOIN (
                SELECT
                    N3.name,
                    N3.lft,
                    N3.rgt,
                    (SELECT COUNT(*) FROM dbo.departments WHERE lft < N3.lft AND rgt > N3.lft) AS depth
                FROM
                    dbo.departments N3) SQ2 ON
        SQ2.lft <= N1.lft AND SQ2.rgt >= N1.lft AND
        SQ2.depth = SQ1.depth AND
        SQ2.name > SQ1.name
    )

如果您遇到任何中断的情况,请告诉我。

I think I finally came up with an ANSI SQL solution. The basic gist is that it counts the number of rows that either have parents with a lower valued name on the same level as one of the node's own parent or is itself on the same level as the node and has a lower valued name. You'll need to make a minor adjustment to it in order to add the indenting if you want it. Also, I don't know how performance on a large data set will be due to all of the subqueries:

SELECT
    N1.name
FROM
    dbo.departments N1
ORDER BY
    (
    SELECT
        COUNT(DISTINCT N2.lft)
    FROM
        dbo.departments N2
    INNER JOIN (
                SELECT
                    N.name,
                    N.lft,
                    N.rgt,
                    (SELECT COUNT(*) FROM dbo.departments WHERE lft < N.lft AND rgt > N.lft) AS depth
                FROM
                    dbo.departments N) SQ1 ON
        SQ1.lft <= N2.lft AND SQ1.rgt >= N2.lft
    INNER JOIN (
                SELECT
                    N3.name,
                    N3.lft,
                    N3.rgt,
                    (SELECT COUNT(*) FROM dbo.departments WHERE lft < N3.lft AND rgt > N3.lft) AS depth
                FROM
                    dbo.departments N3) SQ2 ON
        SQ2.lft <= N1.lft AND SQ2.rgt >= N1.lft AND
        SQ2.depth = SQ1.depth AND
        SQ2.name > SQ1.name
    )

Let me know if you come up with any situations where it breaks.

蒲公英的约定 2024-09-13 09:28:50

下面的内容将适用于您的示例,但如果名称包含“-”字符,则它可能会崩溃。不过,它可以作为一个起点。这使用了 CTE,我相信这是 SQL Server 特有的。如果我想到更通用的 ANSI SQL 方法,我也会发布它。

;WITH Tree_Path AS (
    SELECT
        lft,
        rgt,
        name,
        CAST(name + '-' AS VARCHAR(MAX)) AS tree_path,
        1 AS depth
    FROM
        dbo.departments
    WHERE
        lft = 1
    UNION ALL
    SELECT
        c.lft,
        c.rgt,
        c.name,
        CAST(tp.tree_path + c.name + '-' AS VARCHAR(MAX)),
        tp.depth + 1
    FROM
        Tree_Path tp
    INNER JOIN dbo.departments AS c ON
        c.lft > tp.lft AND
        c.lft < tp.rgt AND
        NOT EXISTS (SELECT * FROM dbo.departments d WHERE d.lft < c.lft AND d.rgt > c.lft AND d.lft > tp.lft AND d.lft < tp.rgt))
SELECT
    REPLICATE('----', depth - 1) + name,
    depth - 1,
    lft
FROM
    Tree_Path
ORDER BY
    tree_path,
    name

The below will work with your example, although if a name contains the "-" character then it might break down. It might serve as a starting point though. This uses CTEs, which are specific to SQL Server I believe. If I think of a more generic ANSI SQL method, I'll post that as well.

;WITH Tree_Path AS (
    SELECT
        lft,
        rgt,
        name,
        CAST(name + '-' AS VARCHAR(MAX)) AS tree_path,
        1 AS depth
    FROM
        dbo.departments
    WHERE
        lft = 1
    UNION ALL
    SELECT
        c.lft,
        c.rgt,
        c.name,
        CAST(tp.tree_path + c.name + '-' AS VARCHAR(MAX)),
        tp.depth + 1
    FROM
        Tree_Path tp
    INNER JOIN dbo.departments AS c ON
        c.lft > tp.lft AND
        c.lft < tp.rgt AND
        NOT EXISTS (SELECT * FROM dbo.departments d WHERE d.lft < c.lft AND d.rgt > c.lft AND d.lft > tp.lft AND d.lft < tp.rgt))
SELECT
    REPLICATE('----', depth - 1) + name,
    depth - 1,
    lft
FROM
    Tree_Path
ORDER BY
    tree_path,
    name
以酷 2024-09-13 09:28:50

问题中有一个不匹配的地方。查询返回:node.name深度node.lft——但结果表标记为:

department      lft     rgt

无论如何,该查询是返回部门级别的正确结果——在这种情况下,这显然不是您想要的深度含义。 ad 都是顶级部门。

如果您想要子部门的数量,并且嵌套集得到正确维护,那么查询很简单:

SELECT 
    D1.name,
    (D1.rgt - D1.lft - 1) / 2    AS SubordinateDepartments
FROM
    departments AS D1
ORDER BY
    SubordinateDepartments DESC,
    D1.name

There is a mismatch in the question. The query returns: node.name, depth, and node.lft -- yet the results table is labeled with:

department      lft     rgt

Anyway, that query is returning the correct results for department level -- which is apparently not what you want depth to mean in this case. Both a and d are top-level departments.

If you want the number of sub-departments, and the nested-set is kept properly maintained, then the query is simply:

SELECT 
    D1.name,
    (D1.rgt - D1.lft - 1) / 2    AS SubordinateDepartments
FROM
    departments AS D1
ORDER BY
    SubordinateDepartments DESC,
    D1.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文