简体   繁体   English

树表菜单的 SQL 查询

[英]SQL query for tree table menu

I have a table with tree structure:我有一个树结构的表:

ID    Title           ParentID     Orderby 
----------------------------------------
1     All             0            2
2     Banking         1            5
3     USAA Checking   0            0
4     USAA Mastercard 1            9
5     Medical         3            0
6     Jobs            3            100
7     Archive         0            1
8     Active          7            0  
9     BoA Amex        1            1

I need to write a SQL query to produce a result like this (ORDER by column Orderby):我需要编写一个 SQL 查询来产生这样的结果(按列 Orderby 排序):

ID    Title           Path         Orderby 
----------------------------------------
3     USAA Checking   1            0
5     Medical         1.1          0
6     Jobs            3.2          100
7     Archive         2            1
8     Active          2.1          0
1     All             3            2
9     BoA Amex        3.1          1
2     Banking         3.2          5
4     USAA Mastercard 3.3          9

Who can help me to write a SQL query?谁能帮我写一个SQL查询? Thanks!谢谢!

We can do this using a recursive CTE:我们可以使用递归 CTE 来做到这一点:

WITH children AS (
    SELECT NULL AS ParentID, ID, Title, Orderby,
        CAST(ID AS VARCHAR(500)) AS Path
    FROM Categories
    WHERE ParentID = 0

    UNION ALL

    SELECT 
       d.ParentID, t.counter + 1, d.ID, d.Title, d.Orderby,
       CAST(CAST(t.Path AS VARCHAR(50)) + '.' +
       CAST(ROW_NUMBER() OVER (PARTITION BY d.ParentID ORDER BY d.ID) AS VARCHAR(50)) AS VARCHAR(500))
    FROM children t
    INNER JOIN Categories AS d
        ON d.ParentID = t.ID
)

SELECT ID, Title, Path, Orderby
FROM children;

在此处输入图片说明

Demo 演示

Note that you never provided fixed logic for what should be used to determine the minor version numbers, for a given parent version.请注意,对于给定的父版本,您从未提供用于确定次要版本号的固定逻辑。 That is, it is not clear why Medical appears earlier than Jobs in the hierarchy.也就是说,尚不清楚为什么Medical在层次结构中比Jobs出现得更早。

You can try below using row_number()您可以在下面尝试使用row_number()

DEMO 演示

select Id,title, concat(val,'.',case when 
row_number() over(partition by val order by Id)-1=0 then null else 
row_number() over(partition by val order by Id)-1 end) as path,
orderby
from
(
select *,case when parentid=0 then id else parentid end as val
from Categories
)A

You Can try Below query if you have one level of the hierarchy如果您有一个层次结构,您可以尝试下面的查询

 Select  
      C.ID as ID, 
      C.Title  as Title, 
      Case 
        when C.ParentID =0 then cast(C.ID as  varchar(2)) 
        else cast(C.ParentID as varchar(2)) + '.' + cast(C.Order as varchar(3)) 
      END as Path, 
      C.Order 
  from Categories as C

You Need to create Temp tables if you have multiple level hierarchy.如果您有多级层次结构,则需要创建临时表。 and you need to update order so that we have a simpler query for the desired output.并且您需要更新订单,以便我们对所需的输出进行更简单的查询。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM