簡體   English   中英

在沒有游標的情況下在SQL中展平SQL分層數據結構

[英]Flattening a SQL hierarchical data structure in SQL without cursors

假設我有下表

SectionID ParentIDSection DatumID
   1           NULL         1
   2            1           2
   3            1           3
   4            2           4

現在,讓我假裝我想選擇SectionID = 1下的所有DatumID,即使它是它的后代孩子的一部分,所以我會得到

SectionID DatumID
  1         1
  1         2
  1         3
  1         4

是否有可能在沒有用游標遞歸地迭代第一個表的情況下?

這讓我困惑了一分鍾,因為它與我對遞歸的看法有相反的方向,但我想出了這個非常簡單的解決方案:

;WITH Rollups AS (
    SELECT SectionId, ParentIdSection, DatumId
    FROM SectionDataTable
    UNION ALL
    SELECT parent.SectionId, parent.ParentIdSection, child.DatumId
    FROM SectionDataTable parent 
    INNER JOIN Rollups child ON child.ParentIdSection = parent.SectionId
)
SELECT *
FROM Rollups 
WHERE SectionID = 1

(替換為您想要的部分ID)

declare @demo table
(
    SectionId int not null primary key clustered
    , ParentId int null --foreign key references @demo(SectionId)
    , DatumId int
);

insert @demo
      select 1, null, 1
union select 2, 1, 2
union select 3, 1, 3
union select 4, 2, 4
union select 5, null, 5
union select 6, 5, 6
;

with demoHierarchy
as
(
    select SectionId
    , SectionId ParentId
    , SectionId OriginalAncestorId
    , DatumId
    from @demo
    where ParentId is null --remove this line if you want OriginalAncestorId to be AnyAncestorId (if that doesn't make sense, try it and see)

    union all

    select d.SectionId
    , d.ParentId
    , h.OriginalAncestorId
    , d.DatumId
    from @demo d
    inner join demoHierarchy h
    on d.ParentId = h.SectionId
    where d.ParentId is not null --implied by the join, but here to make it explicit
)
select OriginalAncestorId SectionId
, DatumId 
from demoHierarchy
where OriginalAncestorId = 1 --not needed given your sample data only contains this data, but required when you have multiple root ancestors

有關分層查詢的更多信息,請訪問: http//blog.sqlauthority.com/2012/04/24/sql-server-introduction-to-hierarchical-query-using-a-recursive-cte-a-primer/

您可以在MS SQL 2008中使用層次結構ID。另一個問題是關於SQL Server 2008層次結構數據類型性能的相對性能?

以下是處理遞歸層次結構的另一個參考: http//www.bimonkey.com/2009/09/handling-recursive-hierarchies-in-sql-server/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM