简体   繁体   English

按层次结构中的级别拆分数据

[英]Split data by levels in hierarchy

Example of initial data: 初始数据示例:

| ID   |  ParentID  |
|------|------------|
|  1   |    NULL    |
|  2   |     1      |
|  3   |     1      |
|  4   |     2      |
|  5   |    NULL    |
|  6   |     2      |
|  7   |     3      |

In my initial data I have ID of element and his parent ID. 在我的初始数据中,我有元素ID和他的父ID。 Some elements has parent, some has not, some has a parent and his parent has a parent. 有些元素有父,有些没有,有些有父母,父母有父母。

The maximum number of levels in this hierarchy is 3. 此层次结构中的最大级别数为3。

I need to get this hierarchy by levels. 我需要按级别获得此层次结构。

Lvl 1 - elements without parents Lvl 2 - elements with parent which doesn't have parent Lvl 3 - elements with parent which has a parent too. Lvl 1 - 没有父母的元素Lvl 2 - 父母没有父Lvl 3元素 - 父母也有父母的元素。

Expected result looks like: 预期结果如下:

| Lvl1  |   Lvl2   |   Lvl3   |
|-------|----------|----------|
|  1    |   NULL   |   NULL   |
|  1    |    2     |   NULL   |
|  1    |    3     |   NULL   |
|  1    |    2     |    4     |
|  5    |   NULL   |   NULL   |
|  1    |    2     |    6     |
|  1    |    3     |    7     |

How I can do it? 我怎么能这样做?

For a fixed dept of three, you can use CROSS APPLY . 对于固定的三个部门,您可以使用CROSS APPLY

It can be used like a JOIN , but also return extra records to give you the NULL s. 它可以像JOIN一样使用,但也返回额外的记录以给你NULL

SELECT
  Lvl1.ID   AS lvl1,
  Lvl2.ID   AS lvl2,
  Lvl3.ID   AS lvl3
FROM
  initial_data   AS Lvl1
CROSS APPLY
(
   SELECT ID FROM initial_data WHERE ParentID = Lvl1.ID
   UNION ALL
   SELECT NULL AS ID
)
  AS Lvl2
CROSS APPLY
(
   SELECT ID FROM initial_data WHERE ParentID = Lvl2.ID
   UNION ALL
   SELECT NULL AS ID
)
  AS Lvl3
WHERE
  Lvl1.ParentID IS NULL
ORDER BY
  Lvl1.ID,
  Lvl2.ID,
  Lvl3.ID

But, as per my comment, this is often a sign that you're headed down a non-sql route. 但是,根据我的评论,这通常表明你正走向非sql路线。 It might feel easier to start with, but later it turns and bites you, because SQL benefits tremendously from normalised structures (your starting data). 开始时可能会感觉更容易,但后来它会转而咬你,因为SQL会从规范化结构(您的起始数据)中获益匪浅。

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

相关问题 在具有层次结构数据的自引用表中,如果字段组在不同层次结构级别上为空白,是否应该拆分表? - In self-referencing table with hierarchical data, should I split the table if groups of fields are blank at different hierarchy levels? MySQL 5.7 对未知级别的层次结构数据的递归查询 - MySQL 5.7 recursive query on hierarchy data with unknown levels 使用PostgreSQL选择层次结构的某些级别 - Selecting certain levels of a hierarchy with PostgreSQL SQL:扁平化层次结构,缺少级别 - SQL: Flatten hierarchy with missing levels 检查1:N层次结构保持在4个级别 - Check 1:N hierarchy is maintained at 4 levels SQL Server 2012:如何从原始表到派生产品表获取多达5个层次结构的数据 - SQL Server 2012: How to get up to 5 hierarchy levels of data from original to derived product table 如何在PostgreSQL中选择具有级别的表的所有层次结构 - How to select all the hierarchy of a table with levels in PostgreSQL 查询层次结构中父级以下几级 - Query how many levels are below a parent in a hierarchy 如何使用 SQLite 按日期查找层次结构中的级别 - How to find levels in a hierarchy by dates using SQLite 如何使用sql定义层次结构中的级别数? - How to define the number of levels in a hierarchy using sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM