SQL 获取最低级别 child 和根节点

SQL Get lowest level child and root node

我有数据库架构:[Id]、[ParrentId]、[更多表]

我的层次结构如下:

1. a
 2. aa
  3. aaa_1
  3. aaa_2
1. b
 2. bb
1. c
 2. cc
  3. ccc_1
   4. cccc
  3. ccc_2

我想要一个 (select * where X) => [X, lowest level child] 比如: [a, aaa_1] [a, aaa_2]; [cc, cccc] 等

我可以用

得到最低的 child
SELECT t1.name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent
WHERE t2.category_id IS NULL;

但我不知道如何加入根节点。

有了SQL服务器,你可以试试这个:

With CTE as 
(
Select ID as Child, lev = 1
from category
where ID = X
UNION ALL
Select category.ID, CTE.lev + 1
from category
    inner join CTE ON category.ParentID = CTE.Child
)

select CTE_1.Child, CTE_2.Child 
from CTE as CTE_1 
    inner join CTE as CTE_2
where CTE_1.lev  = 1 AND CTE_2.lev = (select MAX(CTE.lev) from CTE)  

鉴于:

  • DBMS 是 SQL 服务器;
  • 树的最高层节点有 parent = NULL;
  • 您想要树的所有级别的所有最低叶,而不仅仅是根;
  • 您希望所有节点都处于最低级别,而不仅仅是一个;

这个查询可以做到:

WITH r ( category_id, name, root, depth )
-- finds the root relationship
AS (
    SELECT category_id, name, category_id, 0
        FROM category
        -- WHERE parent IS NULL -- this would only look at root nodes
    UNION ALL
    SELECT c.category_id, c.name, r.root, r.depth + 1
        FROM r
        JOIN category c
            ON c.parent = r.category_id
), s ( category_id, name, root, window_id )
-- finds the lowest leaves
AS (
    SELECT category_id, name, root, RANK() OVER(partition by root order by depth DESC)
        FROM r
)
SELECT c.name AS NodeName, s.Name AS DeepLeafName
    FROM category c
    JOIN s
        ON c.category_id = s.root
        WHERE s.window_id = 1;

这是结果集: